Why doesn’t Kotlin allow you to use lateinit with primitive types?

For (non-nullable) object types, Kotlin uses the null value to mark that a lateinit property has not been initialized and to throw the appropriate exception when the property is accessed. For primitive types, there is no such value, so there is no way to mark a property as non-initialized and to provide the diagnostics that … Read more

Is this really widening vs autoboxing?

In the first case, you have a widening conversion happening. This can be see when runinng the “javap” utility program (included w/ the JDK), on the compiled class: public static void main(java.lang.String[]); Code: 0: iconst_ 5 1: istore_ 1 2: iload_ 1 3: i2l 4: invokestatic #6; //Method hello:(J)V 7: return } Clearly, you see … Read more

Async function returning promise, instead of value [duplicate]

Async prefix is a kind of wrapper for Promises. async function latestTime() { const bl = await web3.eth.getBlock(‘latest’); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == ‘function’); //Returns false – not a promise return bl.timestamp; } Is the same as function latestTime() { return new Promise(function(resolve,success){ const bl = web3.eth.getBlock(‘latest’); bl.then(function(result){ console.log(result.timestamp); // Returns a … Read more

Passing primitives to an OCMock’s stub

It’s been awhile since this question has been asked but I ran into this issue myself and couldn’t find a solution anywhere. OCMock now supports ignoringNonObjectArgs so an example of an expect would be [[[mockObject expect] ignoringNonObjectArgs] someMethodWithPrimitiveArgument:5]; the 5 doesn’t actually do anything, just a filler value

Using int as a type parameter for java.util.Dictionary

In Java primitives aren’t objects, so you can’t use them in place of objects. However Java will automatically box/unbox primitives (aka autoboxing) into objects so you can do things like: List<Integer> intList = new LinkedList<Integer>(); intList.add(1); intList.add(new Integer(2)); … Integer first = intList.get(0); int second = intList.get(1); But this is really just the compiler automatically … Read more

Hashcode of an int

For the hashCode of an int the most natural choice is to use the int itself. A better question is what to use for the hashCode of a long since it doesn’t fit into the int-sized hashcode. Generally, your best source for that—and all hashCode-related questions—would be Effective Java. Here’s what Effective Java recommends (and … Read more

tech