Skip to content

Instantly share code, notes, and snippets.

@optimho
Created January 8, 2022 20:12
Show Gist options
  • Select an option

  • Save optimho/a842967953adf991983e4ac07af63b92 to your computer and use it in GitHub Desktop.

Select an option

Save optimho/a842967953adf991983e4ac07af63b92 to your computer and use it in GitHub Desktop.
type Checking in Kotlin
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val obj: Any = Persons("Bob")
if (obj is String) {
println("It is")
} else {
println("It is not")
println(" the class is a ${obj.javaClass.name}")
}
val obj2: Any = getStuff("2")
if (obj2 is String) {
println("It is")
} else {
println("It is not")
println(" the class is a ${obj2.javaClass.name}")
}
}
fun getStuff(value: String): Any{
return when (value){
"1" -> 1
"2" -> 1.0
"3" -> true
"4" -> "michael"
else -> {-1}
}
}
data class Order (val amount: Int){}
class Persons(name:String){}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment