Skip to content

Instantly share code, notes, and snippets.

@gszeliga
Last active August 29, 2015 13:56
Show Gist options
  • Save gszeliga/8842040 to your computer and use it in GitHub Desktop.
Save gszeliga/8842040 to your computer and use it in GitHub Desktop.
Single Phantom Type
import java.util.Date
trait Translation
trait MissingTranslation
trait AnyTrans extends Translation with MissingTranslation
trait Certificate
trait MissingCertificate
trait AnyCert extends Certificate with MissingCertificate
trait Passport
trait MissingPassport
trait AnyPass extends Passport with MissingPassport
object VisaRequest {
def apply(to: Someone, at: String) = new VisaRequest[MissingTranslation with MissingCertificate with MissingPassport](to, at)
class HasTranslation[FROM, TO]
object HasTranslation {
implicit def hasTranslation[FROM >: AnyCert with AnyPass]: HasTranslation[FROM with MissingTranslation, FROM with Translation] = null
}
class HasCertificate[FROM, TO]
object HasCertificate {
implicit def hasCertificate[FROM >: AnyTrans with AnyPass]: HasCertificate[FROM with MissingCertificate, FROM with Certificate] = null
}
class HasPassport[FROM, TO]
object HasPassport {
implicit def hasPassport[FROM >: AnyTrans with AnyCert]: HasPassport[FROM with MissingPassport, FROM with Passport] = null
}
class VisaRequest[FROM](val to: Someone, val at: String) {
def broughtMarriageTranslation[TO](implicit o: HasTranslation[FROM, TO]) = new VisaRequest[TO](to, at)
def hasUpdatedCensus[TO](implicit o: HasCertificate[FROM, TO]) = new VisaRequest[TO](to, at)
def validPassport[TO](implicit o: HasPassport[FROM, TO]) = new VisaRequest[TO](to, at)
}
class VisaIssuer(private val vr: VisaRequest[Translation with Certificate with Passport]) {
def issue = new Visa(vr.to, new Date(), vr.at)
}
implicit def toIssuer(vr: VisaRequest[Certificate with Translation with Passport]) = {
new VisaIssuer(vr)
}
class Visa(val to: Someone, val expiresAt: Date, val issuedAt: String) {
override def toString = {
s"subject: $to\nexpires_at: $expiresAt\nissued_at: $issuedAt"
}
}
}
class Someone(val name: String, val lastname: String) {
override def toString = s"$lastname, $name"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment