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

Peter

04/16/2020, 7:07 PM
hi, new to kotlin and playing with inline classes as a replacement for value classes
Copy code
inline class TypesafeId<T>(val id: Int)

typealias PersonId = TypesafeId<Person>
data class Person(val id: PersonId)

typealias AnimalId = TypesafeId<Animal>
data class Animal(val id: AnimalId)

fun main(args: Array<String>) {
  println(Animal(AnimalId(1)))
  // println(Person(AnimalId(1))) typecheck fails at compile time 
  println(Person(TypesafeId(1)))
}
wondering why the last
println
compiles successfully?
z

Zach Klippenstein (he/him) [MOD]

04/16/2020, 7:13 PM
The third line in your main function works because the compiler is inferring the
T
of
TypesafeId
to be
Person
.
p

Peter

04/16/2020, 7:14 PM
ahhh ok thanks
d

David Eriksson

04/17/2020, 8:00 AM
#getting-started