https://kotlinlang.org logo
#announcements
Title
# announcements
d

dalexander

02/22/2017, 9:49 PM
@somoni You could use a when statement, I don’t know that it’s actually O(1) but it’s definitely no worse than an if/else chain. ie.
Copy code
val obj: Any = "asdf"
    when(obj) {
        is Int -> println("Int!")
        is Double -> println("Double!")
        is String -> { println("String!") } // you can add braces if you have a longer statement
    }
g

groostav

02/22/2017, 10:31 PM
dalexander: This linear time, not constant time. I believe that with a better pattern matching mechanism and access into the object record's header it can be sped up, but that might be waiting on project-valhala.
2 Views