Skip to content

Instantly share code, notes, and snippets.

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)
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"))
class A
object O:
given A = A()
@main def main =
import O.{given A}
println(summon[A])
import scala.language.implicitConversions
given Conversion[Int, String] = x => x.toString
@main def main =
val str: String = 42
println(str)
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)
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)
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)
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)
class A(x: Int):
def this() = this(42)
@main def main =
println(A())
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]):