Skip to content

scytrowski/mat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mat

Scala Maven Central License: MIT

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.


✨ Features

  • 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] returning Option

📦 Examples

Materialize a literal value

import io.github.scytrowski.mat.*

val x: 42 = materialize[42]
// x: 42

Materialize a tuple

import io.github.scytrowski.mat.*

val x: (true, 'd', "abc") = materialize[(true, 'd', "abc")]
// x: (true, 'd', "abc")

Materialize a case object

import io.github.scytrowski.mat.*

case object SomeObject

val x: SomeObject.type = materialize[SomeObject.type]
// x: SomeObject

Materialize a case class

import io.github.scytrowski.mat.*

case class SomeClass[A](a: A)

val x: SomeClass[15] = materialize[SomeClass[15]]
// x: SomeClass(15)

Materialize a singleton ADT variant

import io.github.scytrowski.mat.*

sealed trait SomeADT

case object SingletonVariant extends SomeADT

val x: SingletonVariant.type = materialize[SomeADT]
// x: SingletonVariant

Provide custom materialization logic

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

Require a materializable type

import io.github.scytrowski.mat.*

def doSomethingWithMaterializableType[A: Materialize] = ???

🚧 TODO

  • Support intersection types - e.g.: 5 & Int should materialize as 5
  • Support union types - e.g.: 5 | String should materialize as 5
  • Support nested singleton ADTs

📄 License

This project is licensed under the MIT License.

You are free to use, copy, modify, and distribute it with attribution.

About

Lightweight Scala 3 library for materializing types into values at compile time.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages