An open API service indexing awesome lists of open source software.

https://github.com/scytrowski/mat

Lightweight Scala 3 library for materializing types into values at compile time.
https://github.com/scytrowski/mat

compiletime scala scala3 typelevel typelevel-programming

Last synced: 18 days ago
JSON representation

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

Awesome Lists containing this project

README

          

# mat

[![Scala](https://img.shields.io/badge/Scala-3.7.3-red.svg)](https://www.scala-lang.org)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.scytrowski/mat_3)](https://mvnrepository.com/artifact/io.github.scytrowski/mat_3)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

**`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

```scala
import io.github.scytrowski.mat.*

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

### Materialize a tuple

```scala
import io.github.scytrowski.mat.*

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

### Materialize a case object

```scala
import io.github.scytrowski.mat.*

case object SomeObject

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

### Materialize a case class

```scala
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

```scala
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

```scala
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

```scala
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](LICENSE).

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