When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

The problem is that GenericTraversableTemplate is used twice: once for mutable collections (where its type parameter should be invariant), and once for immutable collections (where covariance is invariably king). GenericTraversableTemplate’s typechecks assuming either covariance or invariance for the A type parameter. However, when we inherit it in a mutable trait, we have to pick invariance. … Read more

What is unchecked cast and how do I check it?

To elaborate on what Peter wrote: Casts from non-generic types to generic types may work just fine at runtime, because the generic parameters are erased during compilation, so we are left with a legitimate cast. However, the code may fail later with an unexpected ClassCastException due to an incorrect assumption regarding the type parameter. For … Read more

Why dividing int.MinValue by -1 threw OverflowException in unchecked context?

This is not an exception that the C# compiler or the jitter have any control over. It is specific to Intel/AMD processors, the CPU generates a #DE trap (Divide Error) when the IDIV instruction fails. The operating system handles the processor trap and reflects it back into the process with a STATUS_INTEGER_OVERFLOW exception. The CLR … Read more

if checkbox is checked, do this

I would use .change() and this.checked: $(‘#checkbox’).change(function(){ var c = this.checked ? ‘#f00’ : ‘#09f’; $(‘p’).css(‘color’, c); }); — On using this.checked Andy E has done a great write-up on how we tend to overuse jQuery: Utilizing the awesome power of jQuery to access properties of an element. The article specifically treats the use of … Read more

Catch checked change event of a checkbox

<input type=”checkbox” id=”something” /> $(“#something”).click( function(){ if( $(this).is(‘:checked’) ) alert(“checked”); }); Edit: Doing this will not catch when the checkbox changes for other reasons than a click, like using the keyboard. To avoid this problem, listen to changeinstead of click. For checking/unchecking programmatically, take a look at Why isn’t my checkbox change event triggered?

Type safety: Unchecked cast

The problem is that a cast is a runtime check – but due to type erasure, at runtime there’s actually no difference between a HashMap<String,String> and HashMap<Foo,Bar> for any other Foo and Bar. Use @SuppressWarnings(“unchecked”) and hold your nose. Oh, and campaign for reified generics in Java 🙂