This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| opaque type A = Int | |
| object A: | |
| def apply(x: Int): A = x + 10 | |
| extension (x: A) | |
| def toInt: Int = x - 10 | |
| @main def main = | |
| println(A(100).toInt) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.strictEquality | |
| class A[T](val x: T) derives CanEqual | |
| given CanEqual[Int, String] = CanEqual.derived | |
| @main def main = | |
| println(A(1) == A("a")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A | |
| object O: | |
| given A = A() | |
| @main def main = | |
| import O.{given A} | |
| println(summon[A]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.implicitConversions | |
| given Conversion[Int, String] = x => x.toString | |
| @main def main = | |
| val str: String = 42 | |
| println(str) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.compiletime.summonFrom | |
| trait Show[A]: | |
| def show(a: A): String | |
| given Show[String] with | |
| def show(a: String): String = s"String: $a" | |
| inline def show[A](a: A) = summonFrom: | |
| case show: Show[A] => show.show(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| opaque type A = String | |
| object A: | |
| def apply(s: String): A = s | |
| extension (p: A) | |
| def show = s"opaque type A: $p" | |
| @main def main = | |
| println(A("a").show) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension (x: String) | |
| def +: (y: String) = s"$x + ($y)" | |
| def ++ (y: String) = s"$x + ($y)" | |
| @main def main = | |
| println("a" +: "b" +: "c") // a + (b + (c)) | |
| println("a" ++ "b" ++ "c") // a + (b) + (c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.implicitConversions | |
| extension [A](left: A)(using num: Numeric[A]) | |
| def >=(right: A) = num.gteq(left, right) | |
| def <=(right: A) = num.lteq(left, right) | |
| def <== (right: A) = (left <= right, right) | |
| def >== (right: A) = (left >= right, right) | |
| extension [A](temp: (Boolean, A))(using num: Numeric[A]) | |
| def <== (right: A) = (temp._1 && temp._2 <= right, right) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A(x: Int): | |
| def this() = this(42) | |
| @main def main = | |
| println(A()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Show[T]: | |
| def show(value: T): String | |
| given Show[Int] with | |
| def show(value: Int) = s"Int: $value" | |
| given Show[String] with | |
| def show(value: String) = s"String: $value" | |
| class Printer1[T](value: T)(using ctx: Show[T]): |