|
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: |
4 | 3 | 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 |
12 | 5 |
|
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 |
15 | 13 | end Options
|
16 | 14 |
|
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._ |
25 | 17 |
|
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 |
0 commit comments