File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ class LazyList [A ]
2
+
3
+ object LazyList {
4
+ inline implicit def toDeferred [A ](l : LazyList [A ]): Deferred [A ] =
5
+ new Deferred (l)
6
+
7
+ final class Deferred [A ](l : => LazyList [A ]) {
8
+ def #:: [B >: A ](elem : => B ): LazyList [B ] = ???
9
+ }
10
+ }
11
+
12
+ import LazyList ._
13
+
14
+ final class Test {
15
+ lazy val a : LazyList [Int ] = 5 #:: b
16
+ lazy val b : LazyList [Int ] = 10 #:: a
17
+
18
+ val x : LazyList [Int ] = 5 #:: y
19
+ val y : LazyList [Int ] = 10 #:: x // error
20
+ }
Original file line number Diff line number Diff line change
1
+ import scala .language .implicitConversions
2
+
3
+ trait LazyList [+ A ] {
4
+ def isEmpty : Boolean = true
5
+ def head : A
6
+ def tail : LazyList [A ]
7
+ }
8
+
9
+ object LazyList {
10
+ inline implicit def toHelper [A ](l : => LazyList [A ]): Helper [A ] = new Helper (l)
11
+ final class Helper [A ](l : => LazyList [A ]) {
12
+ def #:: [B >: A ](elem : => B ): LazyList [B ] = new LazyList [B ] {
13
+ override def isEmpty : Boolean = false
14
+ override def head : B = elem
15
+ override def tail : LazyList [B ] = l
16
+ }
17
+ }
18
+ }
19
+
20
+ import LazyList ._
21
+
22
+ final class Test1 {
23
+ lazy val a : LazyList [Int ] = 5 #:: b
24
+ val b : LazyList [Int ] = 10 #:: a
25
+
26
+ a.head // ok
27
+ b.head // ok
28
+
29
+ val x : LazyList [Int ] = 5 #:: y
30
+ val y : LazyList [Int ] = 10 #:: x // error
31
+ }
32
+
33
+ final class Test2 {
34
+ lazy val a : LazyList [Int ] = 5 #:: b
35
+ lazy val b : LazyList [Int ] = 10 #:: 30 #:: c
36
+ val c : LazyList [Int ] = 20 #:: a
37
+ }
38
+
39
+ final class Test3 {
40
+ val a : LazyList [Int ] = n #:: a
41
+ a.head
42
+ val n : Int = 20 // error
43
+ }
You can’t perform that action at this time.
0 commit comments