Jakub Kozłowski
Aug 30, 2018 · 1 min read

Let’s look at an example. You have a function:

fun findUserByName(name: String): Something

And you have these (assume Username and Password are newtypes/zero-cost wrappers):

data class Username(value: String)
data class Password(value: String)
data class User(name: Username, pass: Password)

and you want to use a User with findUserByName. Assuming automatic unwrapping is enabled (the solution that you seem to prefer), you can do:

findUserByName(user.name).

However, the compiler won’t help you if you do findUserByName(user.password), which means you don’t have as much type safety as would be possible (e.g. with newtypes in Haskell, or opaque types in Scala 2.13)

    Jakub Kozłowski

    Written by

    I like functional programming and Scala. Usually trying to do both at the same time while keeping the code simple.