Skip to content
/ typeset Public

An Scala type-indexed set, checked at compile time.

License

Notifications You must be signed in to change notification settings

vic/typeset

Repository files navigation

typeset Main workflow

An Scala Type indexed set checked at compile time.

a TypeSet is similar to ZIO's Has[_] type but without any reflection need.

Adding to your project.

Jars are available for all versions and commits at Jitpack.io.

Usage

Creating a TypeSet is always done by prepending something to TNil, which represent the empty TypeSet.

import typeset.*

val x = "Hello" :+: TNil

Of course, being a Set, you can't duplicate types of values on it.

// val bad = "Hello" :+: "World" :+: TNil // This wont compile!!

The type of the set can be re-arrenged on the left side and any combination containing the same types should be valid.

val a: String :+: Boolean :+: Int :+: TNil = "Hello" :+: true :+: 42 :+: TNil
val b: Int :+: String :+: Boolean :+: TNil = a // types are the same.

TypeSets can be joined together if they contained types do not cause duplicates.

val a = 1 :+: TNil
val b = true :+: TNil
val c = a :+: b // Has type Int :+: Boolean :+: TNil

Once created, you can obtain the value of a type from a Set.

val a = true :+: "Hello" :+: TNil
val x: String = a.get[String] // Returns "Hello"

Removing a type returns it's current value and another collapsed set without the type present.

val a = true :+: "Hello" :+: TNil
val (x: String, b) = a.drop[String]() // type of b is Boolean :+: TNil 

That's pretty much it. For more examples you can see the test code.

Contributing

All contributions are welcome, just please be kind and repectful of other people's time.

For Building, you will need mill installed.

mill __.test # This will run all tests