@@ -12,11 +12,10 @@ object Logarithms {
12
12
}
13
13
14
14
// Extension methods define opaque types' public APIs
15
- extension (x : Logarithm ) {
15
+ extension (x : Logarithm )
16
16
def toDouble : Double = math.exp(x)
17
17
def + (y : Logarithm ): Logarithm = Logarithm (math.exp(x) + math.exp(y))
18
- def * (y : Logarithm ): Logarithm = Logarithm (x + y)
19
- }
18
+ def * (y : Logarithm ): Logarithm = x + y
20
19
}
21
20
22
21
object LogTest {
@@ -36,31 +35,42 @@ object Access {
36
35
opaque type PermissionChoice = Int
37
36
opaque type Permission <: Permissions & PermissionChoice = Int
38
37
39
- extension (x : Permissions ) def & (y : Permissions ): Permissions = x & y
40
- extension (x : PermissionChoice ) def | (y : PermissionChoice ): PermissionChoice = x | y
41
- extension (x : Permissions ) def is (y : Permissions ) = (x & y) == y
42
- extension (x : Permissions ) def isOneOf (y : PermissionChoice ) = (x & y) != 0
38
+ extension (x : Permissions )
39
+ def & (y : Permissions ): Permissions = x | y
40
+ extension (x : PermissionChoice )
41
+ def | (y : PermissionChoice ): PermissionChoice = x | y
42
+ extension (granted : Permissions )
43
+ def is (required : Permissions ) = (granted & required) == required
44
+ extension (granted : Permissions )
45
+ def isOneOf (required : PermissionChoice ) = (granted & required) != 0
43
46
44
47
val NoPermission : Permission = 0
45
- val ReadOnly : Permission = 1
46
- val WriteOnly : Permission = 2
47
- val ReadWrite : Permissions = ReadOnly & WriteOnly
48
- val ReadOrWrite : PermissionChoice = ReadOnly | WriteOnly
48
+ val Read : Permission = 1
49
+ val Write : Permission = 2
50
+ val ReadWrite : Permissions = Read | Write
51
+ val ReadOrWrite : PermissionChoice = Read | Write
49
52
}
50
53
51
54
object User {
52
55
import Access ._
53
56
54
57
case class Item (rights : Permissions )
55
58
56
- val x = Item (ReadOnly ) // OK, since Permission <: Permissions
59
+ val roItem = Item (Read ) // OK, since Permission <: Permissions
60
+ val rwItem = Item (ReadWrite )
61
+ val noItem = Item (NoPermission )
57
62
58
- assert( x .rights.is(ReadWrite ) == false )
59
- assert( x .rights.isOneOf(ReadOrWrite ) == true )
63
+ assert( roItem .rights.is(ReadWrite ) == false )
64
+ assert( roItem .rights.isOneOf(ReadOrWrite ) == true )
60
65
61
- // Would be a type error:
62
- // assert( x .rights.isOneOf(ReadWrite ) == true )
66
+ assert( rwItem.rights.is( ReadWrite ) == true )
67
+ assert( rwItem .rights.isOneOf(ReadOrWrite ) == true )
63
68
69
+ assert( noItem.rights.is(ReadWrite ) == false )
70
+ assert( noItem.rights.isOneOf(ReadOrWrite ) == false )
71
+
72
+ // Would be a type error:
73
+ // assert( roItem.rights.isOneOf(ReadWrite) == true )
64
74
}
65
75
66
76
object o {
0 commit comments