io.github.scytrowski.mat to me.cytrowski.mat
mat is a lightweight Scala 3 library for materializing types into values at compile time.
It provides a typeclass-based approach for turning types like tuples, literal types, or case classes into values using inline and Mirror.
- Materialize literal types like
5,"hello",true - Recursively materialize tuples:
(1, "abc", true) - Recursively materialize named tuples:
(a = 1, b = "abc", c = true) - Materialize case classes via
Mirror.ProductOf - Materialize singleton sealed trait based ADTs via
Mirror.SumOf - Safe fallback with
materializeOpt[A]returningOption
import me.cytrowski.mat.*
val x: 42 = materialize[42]
// x: 42import me.cytrowski.mat.*
val x: (true, 'd', "abc") = materialize[(true, 'd', "abc")]
// x: (true, 'd', "abc")import me.cytrowski.mat.*
val x: (a: true, b: 'd', c: "abc") = materialize[(a: true, b: 'd', c: "abc")]
// x: (a = true, b = 'd', c = "abc")import me.cytrowski.mat.*
case object SomeObject
val x: SomeObject.type = materialize[SomeObject.type]
// x: SomeObjectimport me.cytrowski.mat.*
case class SomeClass[A](a: A)
val x: SomeClass[15] = materialize[SomeClass[15]]
// x: SomeClass(15)import me.cytrowski.mat.*
sealed trait SomeADT
case object SingletonVariant extends SomeADT
val x: SingletonVariant.type = materialize[SomeADT]
// x: SingletonVariantimport me.cytrowski.mat.*
sealed abstract class SomeClass
object SomeClass:
val instance: SomeClass = new SomeClass {}
given CustomMaterialize[SomeClass]:
override type Out = SomeClass
override def apply(): SomeClass = SomeClass.instance
val x: SomeClass = materialize[SomeClass]
// x: SomeClass.instanceimport me.cytrowski.mat.*
def doSomethingWithMaterializableType[A: Materialize] = ???- Support intersection types - e.g.:
5 & Intshould materialize as5 - Support union types - e.g.:
5 | Stringshould materialize as5 - Support nested singleton ADTs
This project is licensed under the MIT License.
You are free to use, copy, modify, and distribute it with attribution.