Hello, I'm coming to kotlin from typescript and wa...
# announcements
p
Hello, I'm coming to kotlin from typescript and was wondering how to translate this typescript code to kotlin:
Copy code
// typescript
type ID = String;
function generateId(obj): ID { ...body... }
basically, how to create types like above. i came up with this, but im not sure if its the best way:
Copy code
// kotlin
class ID : String()
fun generateId(obj: Obj): ID { ...body... }
thank you!
d
That shouldn't even compile, since
String
is
final
. Check out type aliases: https://kotlinlang.org/docs/reference/type-aliases.html
­čĹć 1
j
You can use a typealias which will make ID and String equivalent - you'll be able to compare them and they are considered the same class. The other (preferred) option is a data class:
Copy code
data class ID(val id: String)
You'll have to access the value via its id property in this case
p
oh ok, thanks!
b
Another option would be an inline class. They are still an experimental feature though.