hi, new to kotlin and playing with inline classes ...
# announcements
p
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
The third line in your main function works because the compiler is inferring the
T
of
TypesafeId
to be
Person
.
p
ahhh ok thanks
d
#getting-started