From ca9f541de57712543eb13b1652a76d4dec0b2280 Mon Sep 17 00:00:00 2001 From: Philippus Date: Wed, 30 Dec 2020 23:20:19 +0100 Subject: [PATCH] Align Opaque Type Aliases code with docs --- tests/neg/opaque-bounds.scala | 22 ++++++++++------- tests/neg/opaque.scala | 2 +- tests/pos/reference/opaque.scala | 42 ++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/tests/neg/opaque-bounds.scala b/tests/neg/opaque-bounds.scala index 2eb2d7ecc0d1..299d56c3e6e1 100644 --- a/tests/neg/opaque-bounds.scala +++ b/tests/neg/opaque-bounds.scala @@ -19,16 +19,20 @@ object Access { opaque type PermissionChoice = Int opaque type Permission <: Permissions & PermissionChoice = Int - extension (x: Permissions) def & (y: Permissions): Permissions = x & y - extension (x: PermissionChoice) def | (y: PermissionChoice): PermissionChoice = x | y - extension (x: Permissions) def is(y: Permissions) = (x & y) == y - extension (x: Permissions) def isOneOf(y: PermissionChoice) = (x & y) != 0 + extension (x: Permissions) + def & (y: Permissions): Permissions = x | y + extension (x: PermissionChoice) + def | (y: PermissionChoice): PermissionChoice = x | y + extension (granted: Permissions) + def is(required: Permissions) = (granted & required) == required + extension (granted: Permissions) + def isOneOf(required: PermissionChoice) = (granted & required) != 0 val NoPermission: Permission = 0 - val ReadOnly: Permission = 1 - val WriteOnly: Permission = 2 - val ReadWrite: Permissions = ReadOnly & WriteOnly - val ReadOrWrite: PermissionChoice = ReadOnly | WriteOnly + val Read: Permission = 1 + val Write: Permission = 2 + val ReadWrite: Permissions = Read | Write + val ReadOrWrite: PermissionChoice = Read | Write } object User { @@ -39,7 +43,7 @@ object User { val p1: Permissions = ReadOrWrite // error val p2: PermissionChoice = ReadWrite // error - val x = Item(ReadOnly) + val x = Item(Read) assert( x.rights.is(ReadWrite) == false ) assert( x.rights.isOneOf(ReadOrWrite) == true ) diff --git a/tests/neg/opaque.scala b/tests/neg/opaque.scala index de95d81b0466..f702b0047f2e 100644 --- a/tests/neg/opaque.scala +++ b/tests/neg/opaque.scala @@ -43,7 +43,7 @@ object logs { // This is the second way to unlift the logarithm type def toDouble: Double = math.exp(`this`) def +(that: Logarithm): Logarithm = Logarithm(math.exp(`this`) + math.exp(that)) - def *(that: Logarithm): Logarithm = Logarithm(`this` + that) + def *(that: Logarithm): Logarithm = `this` + that } } } diff --git a/tests/pos/reference/opaque.scala b/tests/pos/reference/opaque.scala index cf1271d01382..47b3a273cb1f 100644 --- a/tests/pos/reference/opaque.scala +++ b/tests/pos/reference/opaque.scala @@ -12,11 +12,10 @@ object Logarithms { } // Extension methods define opaque types' public APIs - extension (x: Logarithm) { + extension (x: Logarithm) def toDouble: Double = math.exp(x) def + (y: Logarithm): Logarithm = Logarithm(math.exp(x) + math.exp(y)) - def * (y: Logarithm): Logarithm = Logarithm(x + y) - } + def * (y: Logarithm): Logarithm = x + y } object LogTest { @@ -36,16 +35,20 @@ object Access { opaque type PermissionChoice = Int opaque type Permission <: Permissions & PermissionChoice = Int - extension (x: Permissions) def & (y: Permissions): Permissions = x & y - extension (x: PermissionChoice) def | (y: PermissionChoice): PermissionChoice = x | y - extension (x: Permissions) def is(y: Permissions) = (x & y) == y - extension (x: Permissions) def isOneOf(y: PermissionChoice) = (x & y) != 0 + extension (x: Permissions) + def & (y: Permissions): Permissions = x | y + extension (x: PermissionChoice) + def | (y: PermissionChoice): PermissionChoice = x | y + extension (granted: Permissions) + def is(required: Permissions) = (granted & required) == required + extension (granted: Permissions) + def isOneOf(required: PermissionChoice) = (granted & required) != 0 val NoPermission: Permission = 0 - val ReadOnly: Permission = 1 - val WriteOnly: Permission = 2 - val ReadWrite: Permissions = ReadOnly & WriteOnly - val ReadOrWrite: PermissionChoice = ReadOnly | WriteOnly + val Read: Permission = 1 + val Write: Permission = 2 + val ReadWrite: Permissions = Read | Write + val ReadOrWrite: PermissionChoice = Read | Write } object User { @@ -53,14 +56,21 @@ object User { case class Item(rights: Permissions) - val x = Item(ReadOnly) // OK, since Permission <: Permissions + val roItem = Item(Read) // OK, since Permission <: Permissions + val rwItem = Item(ReadWrite) + val noItem = Item(NoPermission) - assert( x.rights.is(ReadWrite) == false ) - assert( x.rights.isOneOf(ReadOrWrite) == true ) + assert( roItem.rights.is(ReadWrite) == false ) + assert( roItem.rights.isOneOf(ReadOrWrite) == true ) - // Would be a type error: - // assert( x.rights.isOneOf(ReadWrite) == true ) + assert( rwItem.rights.is(ReadWrite) == true ) + assert( rwItem.rights.isOneOf(ReadOrWrite) == true ) + assert( noItem.rights.is(ReadWrite) == false ) + assert( noItem.rights.isOneOf(ReadOrWrite) == false ) + + // Would be a type error: + // assert( roItem.rights.isOneOf(ReadWrite) == true ) } object o {