What does BigInteger.sure() do?

Hi

Reading this nice introduction by Andrey:
http://www.drdobbs.com/jvm/232600836

I could not figure out the meaning of BigInteger.sure():

fun BigInteger.plus(other: BigInteger) = this.add(other).sure()


Even without the sure() the code seems to work. I also was not able to find any Javadoc on it.

Can anybody help?

Thanks and regards, Etienne

By default, kotlin sees any java method's return type and parameter types as nullable (because, for example, no one guarantees the return value is not null). So kotlin sees the method

public BigInteger add(BigInteger value)

as

public add(value : BigInteger?) : BigInteger?

But sometimes library guarantees non null returned value (actually in the example above) or requires non null arguments. In such cases there should be a special header that specifies declarations of java methods (makes some nullable types non null). These special headers are now ready for some part of java standard library, but the common mechanism of creating these headers for other libraries is under development.

Hi Svetlana

Thank you very much for this elaborate explanation. So, I assume by calling the sure() API the ‘same’ value is returned but without the question mark in the signature.

Regards, Etienne

Yep, http://confluence.jetbrains.net/display/Kotlin/Null-safety#Null-safety-sure

But note sure() throws NullPointerException if value is null so this is unsafe feature and you should use it ONLY when you are absolutely sure the value will never null.

For example,

val p = Pattern.compile(“[0-9]+”).sure()


is valid since we now Pattern.compile will never return null.

Yes, I'm aware of that. Thanks, Sergey.