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)
- Materialize case classes via
Mirror.ProductOf
- Materialize singleton sealed trait based ADTs via
Mirror.SumOf
- Safe fallback with
materializeOpt[A]
returningOption
import io.github.scytrowski.mat.*
val x: 42 = materialize[42]
// x: 42
import io.github.scytrowski.mat.*
val x: (true, 'd', "abc") = materialize[(true, 'd', "abc")]
// x: (true, 'd', "abc")
import io.github.scytrowski.mat.*
case object SomeObject
val x: SomeObject.type = materialize[SomeObject.type]
// x: SomeObject
import io.github.scytrowski.mat.*
case class SomeClass[A](a: A)
val x: SomeClass[15] = materialize[SomeClass[15]]
// x: SomeClass(15)
import io.github.scytrowski.mat.*
sealed trait SomeADT
case object SingletonVariant extends SomeADT
val x: SingletonVariant.type = materialize[SomeADT]
// x: SingletonVariant
import io.github.scytrowski.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.instance
import io.github.scytrowski.mat.*
def doSomethingWithMaterializableType[A: Materialize] = ???
- Support intersection types - e.g.:
5 & Int
should materialize as5
- Support union types - e.g.:
5 | String
should 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.