From facd751b111ec86c248641449e57576a87455633 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 4 Oct 2021 13:24:04 +0200 Subject: [PATCH] More lazylist tests Bring back LazyCons, since that version also works now under the new rules --- .../pos-custom-args/captures/lazylists.scala | 8 +++-- .../pos-custom-args/captures/lazylists1.scala | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/pos-custom-args/captures/lazylists1.scala diff --git a/tests/pos-custom-args/captures/lazylists.scala b/tests/pos-custom-args/captures/lazylists.scala index 0f12bb4f7b0f..5c8e5e723e2d 100644 --- a/tests/pos-custom-args/captures/lazylists.scala +++ b/tests/pos-custom-args/captures/lazylists.scala @@ -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" @@ -35,4 +36,7 @@ def test(cap1: Cap, cap2: Cap) = def isEmpty = false def head = f("") def tail = LazyNil - new Initial \ No newline at end of file + new Initial + val xsc: {cap1} LazyList[String] = xs + val ys = xs.map(g) + val ysc: {cap1, cap2} LazyList[String] = ys diff --git a/tests/pos-custom-args/captures/lazylists1.scala b/tests/pos-custom-args/captures/lazylists1.scala new file mode 100644 index 000000000000..0fd1e767f49b --- /dev/null +++ b/tests/pos-custom-args/captures/lazylists1.scala @@ -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