In Haskell we can declare newtypes to reduce dynamic memory allocations and create type-safe APIs.
newtype Username String = Username String
deriving (Eq, Ord)
newtype works only with data types which have single constructor with single argument. Is there something similar to newtype in Scala?
Example:
case class Username(username: String)
for username we could've write something like this in Scala:
newtype Username(username: String)
The equivalent to Haskell's newtype
areOpaque Type Aliases in Scala 3:
opaque type Username = String
It's basically a type
alias … but opaque. Just like it says on the cover.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.