Suggestion: replace "lateinit" with ! after type

I come to Kotlin from Swift (and Java of course). One minor syntax thing about Swift that I like better than Kotlin is the use of the ! after the type in var definitions instead of the ugly (IMO) “lateinit” keyword. (Swift calls the feature “Implicitly Unwrapped Optionals”.) I realize this is a breaking change, so it won’t happen soon, but maybe for version 2?

FWIW, in general, I like Kotlin much more than Swift. I appreciate the Kotlin designers reluctance to add features just for the sake of adding features. Keep up the good work. :+1:

1 Like

Unfortunately, ! is reserved in Kotlin for “platform types” (non-denotable). Moreover, in Kotlin (unlike Swift), lateinit is not a part of a type, but rather a part of a declaration. E.g., in Kotlin a function cannot return “lateinit value” (but in Swift you can return Int!) – the returned value in Kotlin must be either nullable or not. However, a variable can be marked as lateinit for the purpose of solving the late initialization problem (hence the name), which is the same problem that Swift’s implicitly unwrapped optional are designed to solve.

P.S. If Kotlin’s platform types (like String!) were made denotable, they would have behaved similarly to Swift’s implicitly unwrapped optionals from a user stand-point. However, the design decision in Kotlin is to limit such types for Java interop only and to enforce type-safety thought the Kotlin code as much as possible. Kotlin tries to be explicit and lateinit is just the necessary compromise.

1 Like

That makes sense. That was a good choice to make lateinit not part of the type. Type mismatches in Swift are one giant PITA.

1 Like

I like it the way it is, however if there were a special syntax for this case, I wouldn’t use ! since for me it would express something urgent like “fire!” and not something deferred.