Skip to content

Align Opaque Type Aliases code with docs #10955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions tests/neg/opaque-bounds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/opaque.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
42 changes: 26 additions & 16 deletions tests/pos/reference/opaque.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,31 +35,42 @@ 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 {
import Access._

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 {
Expand Down