Skip to content

Instantly share code, notes, and snippets.

@folex
Last active May 25, 2017 07:54
Show Gist options
  • Save folex/9f79a97ba01f4397d8dcf27c9d112af3 to your computer and use it in GitHub Desktop.
Save folex/9f79a97ba01f4397d8dcf27c9d112af3 to your computer and use it in GitHub Desktop.
shapeless_tags_fun.scala
object UserRelationsT {
import shapeless.tag
import tag.@@
import cats.data.Ior
trait IsFollowerTag
type IsFollower = Boolean @@ IsFollowerTag
def isFollower(b: Boolean): IsFollower = tag[IsFollowerTag][Boolean](b)
trait IsFollowingTag
type IsFollowing = Boolean @@ IsFollowingTag
def isFollowing(b: Boolean): IsFollowing = tag[IsFollowingTag][Boolean](b)
type Rel = Ior[IsFollowing, IsFollower]
implicit class OptionTagOps[U, I](opt: Option[U @@ I]) {
def getOrElseT[B >: U](default: B): @@[B, I] = {
opt.getOrElse(tag[I][B](default))
}
}
val example1 = Ior.left(isFollower(true)).left.getOrElseT(false) //works fine
val casted1: IsFollower = example1 //works fine
val example2: IsFollower = Ior.left(isFollower(true)).left.getOrElseT(false) //compilation error
/*
md70.sc:1: type mismatch;
found : Boolean(false)
required: shapeless.tag.Tagged[$sess.cmd62.IsFollowerTag] with Boolean
val example2: IsFollower = Ior.left(isFollower(true)).left.getOrElseT(false)
^
*/
}
@folex
Copy link
Author

folex commented May 24, 2017

What I want here is such getOrElse that is able to wrap result in expected @@

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment