https://kotlinlang.org logo
Title
h

Hullaballoonatic

09/17/2018, 6:29 PM
guess i can just use enums or sealed classes instead
s

Sam

09/17/2018, 6:35 PM
Not sure if this helps but something that came across, https://kotlinlang.org/docs/reference/generics.html#upper-bounds
fun <T> copyWhenGreater(list: List<T>, threshold: T): List<String>
    where T : CharSequence,
          T : Comparable<T> {
    return list.filter { it > threshold }.map { it.toString() }
}
h

Hullaballoonatic

09/17/2018, 6:36 PM
this does help, thank you
this seems to be exactly what i wanted
s

Sam

09/17/2018, 6:37 PM
Cool
open class Animal
interface Carnivorous
class Cat : Animal()
class Dog : Animal()
class Pig : Animal()
class Lion : Animal(), Carnivorous
class Tiger : Animal(), Carnivorous

fun <T> printCarnivorousAnimal( animal : T ) where T : Animal, T : Carnivorous {
    println( animal )
}

fun main(args: Array<String>) {
    
    printCarnivorousAnimal( Lion() )
    printCarnivorousAnimal( Tiger() )
    printCarnivorousAnimal( Cat() ) // error as Cat doesn't implement Carnivorous
}
Helps enforce type safety
h

Hullaballoonatic

09/17/2018, 6:56 PM
yup
love you, dude. great help!
s

Sam

09/17/2018, 6:57 PM
No worries
a

Andreas Sinz

09/17/2018, 7:15 PM
@Hullaballoonatic are you sure thats what you wanted? that just means
T
has to be
CharSequence
AND
Comparable<T>
h

Hullaballoonatic

09/17/2018, 7:16 PM
yeah, it's not exactly what i needed, but i still appreciate that you showed me something i don't already understand
👍 1