microposa.blogg.se

Kotlin any type
Kotlin any type









kotlin any type

Nothing lies at the bottom of the type system and is a subclass of every type so it is easy to actually assign it to any other type so that branches which throws an exception are correct from type system viewpoint. How you could assign throwing an exception to a variable? What is a type of such variable? val d: () -> Nothing = Now imagine that one branch of this if would end with an exception val res: Int = if(cond) 1 else throw Exception(.) In Kotlin an if is an expression so you can assign it to a value val cond=Math.random() > 0.5 val res: Int = if(cond) 1 else 2

kotlin any type

Why complete type system is so important? Without it you are unable to treat each expression as a value because what would be the value of : throw new RuntimeException(.) In this section we will focus on explaining how Kotlin Type System is built and how not having 'holes' in a system allow use to tread each instruction as a an expression which has a value.











Kotlin any type