Skip to content

Commit 93df033

Browse files
committed
Improve code examples in opaques.md
1 parent a8a45d0 commit 93df033

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

docs/docs/reference/other-new-features/opaques.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ l / l2 // error: `/` is not a member of Logarithm
6161
### Bounds For Opaque Type Aliases
6262

6363
Opaque type aliases can also come with bounds. Example:
64+
6465
```scala
6566
object Access:
6667

@@ -85,11 +86,12 @@ object Access:
8586

8687
end Access
8788
```
89+
8890
The `Access` object defines three opaque type aliases:
8991

90-
- `Permission`, representing a single permission,
91-
- `Permissions`, representing a set of permissions with the meaning "all of these permissions granted",
92-
- `PermissionChoice`, representing a set of permissions with the meaning "at least one of these permissions granted".
92+
- `Permission`, representing a single permission,
93+
- `Permissions`, representing a set of permissions with the meaning "all of these permissions granted",
94+
- `PermissionChoice`, representing a set of permissions with the meaning "at least one of these permissions granted".
9395

9496
Outside the `Access` object, values of type `Permissions` may be combined using the `&` operator,
9597
where `x & y` means "all permissions in `x` *and* in `y` granted".
@@ -106,6 +108,7 @@ All three opaque type aliases have the same underlying representation type `Int`
106108
`Permission` type has an upper bound `Permissions & PermissionChoice`. This makes
107109
it known outside the `Access` object that `Permission` is a subtype of the other
108110
two types. Hence, the following usage scenario type-checks.
111+
109112
```scala
110113
object User:
111114
import Access._
@@ -116,16 +119,17 @@ object User:
116119
val rwItem = Item(ReadWrite)
117120
val noItem = Item(NoPermission)
118121

119-
assert( roItem.rights.is(ReadWrite) == false )
120-
assert( roItem.rights.isOneOf(ReadOrWrite) == true )
122+
assert(!roItem.rights.is(ReadWrite))
123+
assert(roItem.rights.isOneOf(ReadOrWrite))
121124

122-
assert( rwItem.rights.is(ReadWrite) == true )
123-
assert( rwItem.rights.isOneOf(ReadOrWrite) == true )
125+
assert(rwItem.rights.is(ReadWrite))
126+
assert(rwItem.rights.isOneOf(ReadOrWrite))
124127

125-
assert( noItem.rights.is(ReadWrite) == false )
126-
assert( noItem.rights.isOneOf(ReadOrWrite) == false )
128+
assert(!noItem.rights.is(ReadWrite))
129+
assert(!noItem.rights.isOneOf(ReadOrWrite))
127130
end User
128131
```
132+
129133
On the other hand, the call `roItem.rights.isOneOf(ReadWrite)` would give a type error
130134
since `Permissions` and `PermissionChoice` are different, unrelated types outside `Access`.
131135

0 commit comments

Comments
 (0)