Created
January 8, 2022 20:25
-
-
Save optimho/59a0807608143fdf228689ebe74003ef to your computer and use it in GitHub Desktop.
returning an any type structure and casting it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.temporal.TemporalAmount | |
| import kotlin.reflect.typeOf | |
| fun main(args: Array<String>) { | |
| val obj: Any = getStuff("5") | |
| val casted: Persons = obj as Persons | |
| println(casted.name) | |
| } | |
| fun getStuff(value: String): Any{ | |
| return when (value){ | |
| "1" -> 1 | |
| "2" -> 1.0 | |
| "3" -> true | |
| "4" -> "michael" | |
| "5" -> Persons("Monkey") | |
| else -> {-1} | |
| } | |
| } | |
| data class Order (val amount: Int){} | |
| data class Persons(val name:String){} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment