Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/docs/reference/other-new-features/opaques.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,28 @@ object User {
On the other hand, the call `roItem.rights.isOneOf(ReadWrite)` would give a type error
since `Permissions` and `PermissionChoice` are different, unrelated types outside `Access`.

## Opaque as new types

Sometimes one might want to use opaque types as new types but having to declare `apply` and `value` methods is too much boilerplate.

```scala

trait NewType[Wrapped] {
opaque type Type = Wrapped
def apply(w: Wrapped): Type = w
extension (t: Type) def unwrap: Wrapped = t
}

object Username extends NewType[String]
extension (username: Username.Type) def screenName: String = "@" + username.unwrap

object Token extends NewType[String]

object UserId extends NewType[String]

@main def main() =
println(Username("odesky").screenName)

```

[More details](opaques-details.md)