-
Notifications
You must be signed in to change notification settings - Fork 22
Kotlin Basics: Large tree of condition statements
Devrath edited this page Dec 24, 2023
·
1 revision
val finalOutput = if( ... )
{
...
//returns String
}else if( ... ){
...
//returns Int
}else if( ... ){
...
//returns Double
}
- We can observe that each block returns a different datatype - > The way
kotlin
compiler resolves this is it has anAny()
type meaning return type can be any of the types. - The way compiler does is the final result will become the data type of the tree.
- This is because, once we define a type while defining the large nested condition, the error will be thrown.
val finalOutput : String = if( ... )
{
...
//returns String
}else if( ... ){
...
//returns Int | - > Error is thrown
}else if( ... ){
...
//returns Double | - > Error is thrown
}