Skip to content

Instantly share code, notes, and snippets.

@hobwekiva
Created February 17, 2021 23:19
Show Gist options
  • Save hobwekiva/e888e89b8c2024af6369fd596169e4ba to your computer and use it in GitHub Desktop.
Save hobwekiva/e888e89b8c2024af6369fd596169e4ba to your computer and use it in GitHub Desktop.
sealed class StableName[A] extends Serializable
object StableName {
private[this] final class RefInstance[A <: AnyRef](val value: A) extends StableName[A] {
override def clone(): AnyRef = this
override def equals(obj: Any): Boolean = obj match {
case that: RefInstance[_] => that.value eq this.value
case _ => false
}
override def hashCode(): Int =
System.identityHashCode(value)
}
private[this] final case object UnitInstance extends StableName[Unit]
private[this] final case object NullInstance extends StableName[Unit]
private[this] final case class ByteInstance(value: Byte) extends StableName[Byte]
private[this] final case class ShortInstance(value: Short) extends StableName[Short]
private[this] final case class IntInstance(value: Int) extends StableName[Int]
private[this] final case class LongInstance(value: Long) extends StableName[Long]
private[this] final case class FloatInstance(value: Float) extends StableName[Float]
private[this] final case class DoubleInstance(value: Double) extends StableName[Double]
def makeFromRef[A <: AnyRef](value: A): UIO[StableName[A]] =
UIO { new RefInstance[A](value) }
def make[A](value: A): UIO[StableName[A]] =
UIO {
value match {
case () => UnitInstance.asInstanceOf[StableName[A]]
case null => NullInstance.asInstanceOf[StableName[A]]
case v: Byte => ByteInstance(v).asInstanceOf[StableName[A]]
case v: Short => ShortInstance(v).asInstanceOf[StableName[A]]
case i: Int => IntInstance(i).asInstanceOf[StableName[A]]
case i: Long => LongInstance(i).asInstanceOf[StableName[A]]
case i: Float => FloatInstance(i).asInstanceOf[StableName[A]]
case i: Double => DoubleInstance(i).asInstanceOf[StableName[A]]
case v: AnyRef => new RefInstance(v).asInstanceOf[StableName[A]]
case _ => sys.error("impossible")
}
}
implicit def eqInstance[A]: Eq.Univ[StableName[A]] =
Eq.fromUniversalEquals[StableName[A]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment