diff --git a/compiler/test/dotc/pos-test-pickling.blacklist b/compiler/test/dotc/pos-test-pickling.blacklist index 88489a38c9c4..104029b39c94 100644 --- a/compiler/test/dotc/pos-test-pickling.blacklist +++ b/compiler/test/dotc/pos-test-pickling.blacklist @@ -9,6 +9,7 @@ t3249 t3486 t3612.scala reference +scala-days-2019-slides # type of super reference changes due to late addition of Mirror.Singleton i939.scala diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-1-forset.scala b/tests/pos/scala-days-2019-slides/metaprogramming-1-forset.scala new file mode 100644 index 000000000000..cbb48e1958a0 --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-1-forset.scala @@ -0,0 +1,14 @@ +object ForSetExample { + + import scala.collection.immutable._ + + inline def setFor[T]: Set[T] = + implicit match { + case ord: Ordering[T] => new TreeSet[T] + case _ => new HashSet[T] + } + + setFor[String] // new TreeSet(scala.math.Ordering.String) + setFor[Object] // new HashSet + +} diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-1-logger.scala b/tests/pos/scala-days-2019-slides/metaprogramming-1-logger.scala new file mode 100644 index 000000000000..94d340efc7b0 --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-1-logger.scala @@ -0,0 +1,30 @@ +object Logger { + + var indent = 0 + + inline def log[T](msg: String)(op: => T): T = { + println(s"${" " * indent}start $msg") + indent += 1 + val result = op + indent -= 1 + println(s"${" " * indent}$msg = $result") + result + } +} + +object Logger2 { + + private var indent = 0 +// def inline$indent: Int = indent +// def inline$indent_=(x$0: Int): Unit = indent = x$0 + + inline def log[T](msg: String)(op: => T): T = { + println(s"${" " * indent}start $msg") + indent += 1 // inline$indent = inline$indent + 1 + + val result = op + indent -= 1 // inline$indent = inline$indent - 1 + println(s"${" " * indent}$msg = $result") + result + } +} diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-1-match.scala b/tests/pos/scala-days-2019-slides/metaprogramming-1-match.scala new file mode 100644 index 000000000000..62e68f7811ca --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-1-match.scala @@ -0,0 +1,14 @@ +object App { + + trait Nat + case object Zero extends Nat + case class Succ[N <: Nat](n: N) extends Nat + + inline def toInt(n: => Nat): Int = inline n match { + case Zero => 0 + case Succ(n1) => toInt(n1) + 1 + } + + val natTwo = toInt(Succ(Succ(Zero))) + +} diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-1-power.scala b/tests/pos/scala-days-2019-slides/metaprogramming-1-power.scala new file mode 100644 index 000000000000..cc80782c87aa --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-1-power.scala @@ -0,0 +1,72 @@ +object App2 { + + inline def power(x: Long, n: Int): Long = { + if (n == 0) + 1L + else if (n % 2 == 1) + x * power(x, n - 1) + else { + val y: Long = x * x + power(y, n / 2) + } + } + + val x: Long = 5L + + power(x, 10) + +// def badPower(x: Long, n: Int): Long = { +// power(x, n) // error: Maximal number of successive inlines (32) exceeded, Maybe this is caused by a recursive inline method? +// } + +} + +object App3 { + + inline def power(x: Long, inline n: Int): Long = { + if (n == 0) + 1L + else if (n % 2 == 1) + x * power(x, n - 1) + else { + val y: Long = x * x + power(y, n / 2) + } + } + + val x: Long = 5L + val n: Int = 10 + + power(x, 10) + +// def badPower(x: Long, n: Int): Long = { +// power(x, n) // error: argument to inline parameter must be a known value +// } + +} + + +object App4 { + + inline def power(x: Long, n: Int): Long = { + inline if (n == 0) + 1L + else inline if (n % 2 == 1) + x * power(x, n - 1) + else { + val y: Long = x * x + power(y, n / 2) + } + } + + val x: Long = 5L + val n: Int = 10 + + power(x, 10) + +// def badPower(x: Long, n: Int): Long = { +// power(x, n) // error: cannot reduce inline if +// } + +} + diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-2-addEventListener.scala b/tests/pos/scala-days-2019-slides/metaprogramming-2-addEventListener.scala new file mode 100644 index 000000000000..b7489c2dc887 --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-2-addEventListener.scala @@ -0,0 +1,17 @@ +object JS { + + object dom { + type Event + type MouseEvent <: Event + } + + type EventTypeOf[Tp <: String] <: dom.Event = Tp match { + case "click" => dom.MouseEvent + case _ => dom.Event + } + + def addEventListener[Tp <: String, Ev <: EventTypeOf[Tp]](tpe: Tp)(e: Ev => Any): Unit = ??? + + addEventListener("click") { (e: dom.MouseEvent) => ??? } + +} diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-2-elem.scala b/tests/pos/scala-days-2019-slides/metaprogramming-2-elem.scala new file mode 100644 index 000000000000..745bf8fdba6d --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-2-elem.scala @@ -0,0 +1,14 @@ +object ElemExample { + + type Elem[X] = X match { + case String => Char + case Array[t] => t + case Iterable[t] => t + } + + + the[Elem[String] =:= Char] + the[Elem[Array[Int]] =:= Int] + the[Elem[List[Float]] =:= Float] + the[Elem[Nil.type] =:= Nothing] +} diff --git a/tests/pos/scala-days-2019-slides/metaprogramming-2-tuple.scala b/tests/pos/scala-days-2019-slides/metaprogramming-2-tuple.scala new file mode 100644 index 000000000000..be2cc6fe0147 --- /dev/null +++ b/tests/pos/scala-days-2019-slides/metaprogramming-2-tuple.scala @@ -0,0 +1,13 @@ +object TupleExample { + import Tuple._ + + type A + type B + type C + + the[Concat[A *: B *: Unit, C *: Unit] =:= A *: B *: C *: Unit] + + the[Concat[A *: B *: Unit, C *: Tuple] =:= A *: B *: C *: Tuple] + + the[Concat[A *: B *: Tuple, C *: Unit] <:< A *: B *: Tuple] +}