Kotlin "to" function and null

I am a Kotlin newbie , i am slightly confused with nullable types, my main confusion is in understanding the infix to function. The to function is defined as :

public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)

As i understand , a nullable type is declared as “T?” and as per the above definition A is not a nullable type.

But in the IDE, when i write a statement like : val test = null to “NULL”
it compiles and produces a Pair<Nothing?,String>

Why does it produce “Nothing?” why not any other nullable type Like “Unit?” or “Any?”

Secondly if i were to write my own infix “to” which would prohibit nulls how would i declare that ?

I am enjoying kotlin by the way, sorry if this question has been answered before.

Regards,
Amar

Generic type parameters without constraints imply nullability. If you do not want nulls you have to say it explicitly:

fun <A : Any> foo(value: A, nullableValue: A?)

Note that the second parameter has nullability mark since A is non-nullable now.

As for null value, if I undestand it correctly, it has to have a type which is subtype of all other nullable types in order to be assignable to any variable with nullable type. This type is exactly Nothing?.

Not quite. A simple type parameter can be filled in by either a nullable or non-nullable type. If you don’t want the type to be nullable you can do so by making it inherit Any (eg. class Example<T:Any>(val member:T). If you specify T? as parameter or return type it means that independent of the nullability of T the value concerned is nullable.