Skip to content

Commit

Permalink
More lazylist tests
Browse files Browse the repository at this point in the history
Bring back LazyCons, since that version also works now under the new rules
  • Loading branch information
odersky committed Oct 4, 2021
1 parent c568038 commit facd751
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tests/pos-custom-args/captures/lazylists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ extension [A](xs: {*} LazyList[A])
def head: B = f(xs.head)
def tail: {this} LazyList[B] = xs.tail.map(f) // OK
def concat(other: {f} LazyList[A]): {this, f} LazyList[A] = ??? : ({xs, f} LazyList[A]) // OK
new Mapped
if xs.isEmpty then LazyNil
else new Mapped

def test(cap1: Cap, cap2: Cap) =
def f(x: String): String = if cap1 == cap1 then "" else "a"
Expand All @@ -35,4 +36,7 @@ def test(cap1: Cap, cap2: Cap) =
def isEmpty = false
def head = f("")
def tail = LazyNil
new Initial
new Initial
val xsc: {cap1} LazyList[String] = xs
val ys = xs.map(g)
val ysc: {cap1, cap2} LazyList[String] = ys
35 changes: 35 additions & 0 deletions tests/pos-custom-args/captures/lazylists1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class CC
type Cap = {*} CC

trait LazyList[+A]:
this: ({*} LazyList[A]) =>

def isEmpty: Boolean
def head: A
def tail: {this} LazyList[A]

object LazyNil extends LazyList[Nothing]:
def isEmpty: Boolean = true
def head = ???
def tail = ???

class LazyCons[+T](val x: T, val xs: {*} () => {*} LazyList[T]) extends LazyList[T]:
this: ({*} LazyList[T]) =>

def isEmpty = false
def head = x
def tail: {this} LazyList[T] = xs()

extension [A](xs: {*} LazyList[A])
def map[B](f: {*} A => B): {xs, f} LazyList[B] =
if xs.isEmpty then LazyNil
else LazyCons(f(xs.head), () => xs.tail.map(f))

def test(cap1: Cap, cap2: Cap) =
def f(x: String): String = if cap1 == cap1 then "" else "a"
def g(x: String): String = if cap2 == cap2 then "" else "a"

val xs = LazyCons("", () => if f("") == f("") then LazyNil else LazyNil)
val xsc: {cap1} LazyList[String] = xs
val ys = xs.map(g)
val ysc: {cap1, cap2} LazyList[String] = ys

0 comments on commit facd751

Please sign in to comment.