Skip to content

Commit 9a6abd0

Browse files
Clean up example 4, make version without inner class
1 parent 5af6287 commit 9a6abd0

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
inline trait Options[T]:
2-
// FIXME: remove original class definition from inline trait to allow public classes
3-
private trait Option:
1+
inline trait Options[+T]:
2+
sealed trait Option:
43
def get: T
5-
// FIXME: support constructor parameter
6-
// FIXME: support specialized parents
7-
private class Some://(x: T): // extends Option
8-
def get: T = ??? // x
9-
// FIXME: support specialized modules
10-
// private object None // extends Option
11-
// def get: T = throw new NoSuchElementException("None.get")
4+
def isEmpty: Boolean
125

13-
// FIXME: specialize reference to Option and Some
14-
// def option(value: T): Option = new Some//(value)
6+
class Some(x: T) extends Option:
7+
def get: T = x
8+
def isEmpty: Boolean = false
9+
10+
object None extends Option:
11+
def get: T = throw new NoSuchElementException("None.get")
12+
def isEmpty: Boolean = true
1513
end Options
1614

17-
object IntOptions extends Options[Int]:
18-
/*
19-
<generated> trait Option:
20-
def get: Int
21-
<generated> class Some(x: Int) extends Option
22-
def get: Int = x
23-
<generated> object None extends Option
24-
def get: Int = throw new NoSuchElementException("None.get")
15+
object IntOptions extends Options[Int]
16+
import IntOptions._
2517

26-
<generated> def option(value: Int): Option = new Some(value)
27-
*/
28-
end IntOptions
18+
val o1: Option = Some(1) // specialized
19+
val o2: Option = None
20+
val x1: Int = o1.get // no unboxing
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
inline trait Option[+T]:
2+
def get: T
3+
def isEmpty: Boolean
4+
end Option
5+
6+
inline trait Some[+T](x: T) extends Option[T]:
7+
def get: T = x
8+
def isEmpty: Boolean = false
9+
end Some
10+
11+
inline trait None extends Option[Nothing]:
12+
def get: Nothing = throw new NoSuchElementException("None.get")
13+
def isEmpty: Boolean = true
14+
end None
15+
16+
sealed trait IntOption extends Option[Int]
17+
class IntSome(i: Int) extends IntOption, Some[Int](i)
18+
object IntNone extends IntOption, None
19+
20+
val o1: IntOption = IntSome(1) // specialized
21+
val o2: IntOption = IntNone
22+
val o3: Some[Int] = IntSome(1) // non-specialized
23+
val x1: Int = o1.get // no unboxing
24+
val x3: Int = o3.get // unboxing

0 commit comments

Comments
 (0)