Opaques

This is mini project to create opaque types, and companion objects.

It's Scala 3 only.

To install

libraryDependencies += "io.github.mercurievv.minuscles" % "opaques" % "0.2.0"

Use import

import io.github.mercurievv.minuscles.opaques.*

Let's create opaque type:

val UserName = Opaque.create[String]
// UserName: Opaque[String] = io.github.mercurievv.minuscles.opaques.Opaque$$anon$1@795c77ff
type UserName = UserName.Opq

Now we will do some mappings:

Opaque object creation and getting back

val userName = UserName("MercurieVV")
// userName: Opq = "MercurieVV"
val userNameStr = userName.value
// userNameStr: String = "MercurieVV"
assert(userNameStr == "MercurieVV")

Opaque functor

Create functor to mark input data

object Input extends OpaqueFunctor
type Input[a] = Input.Opq[a]

Now wrap some value

val inputInt: Input[Int] = Input(5)
// inputInt: Opq[Int] = 5

Getting original value

val myInt: Int = inputInt.value
// myInt: Int = 5

Input[Int] is a distinct type from Int — they are not interchangeable:

val notAnInt: Int = inputInt //won't compile
// error: 
// Found:    (repl.MdocSession.MdocApp.inputInt : repl.MdocSession.MdocApp.Input.Opq[Int])
// Required: Int