Created
June 29, 2016 09:32
-
-
Save satyagraha/467ce5c77b5d62c6aa45d5d500f0a4e4 to your computer and use it in GitHub Desktop.
Minor reformatting of https://gist.github.com/Baccata/8fea4427923d1fa0c180
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.shape | |
object TypeEqualityChecks { | |
trait Bool { | |
type not <: Bool | |
} | |
trait True extends Bool { | |
type not = False | |
} | |
trait False extends Bool { | |
type not = True | |
} | |
//Defining | |
def unexpected: Nothing = sys.error("Unexpected invocation") | |
trait =:!=[A, B] | |
implicit def neq[A, B]: A =:!= B = new =:!=[A, B] {} | |
implicit def neqAmbig1[A]: A =:!= A = unexpected | |
implicit def neqAmbig2[A]: A =:!= A = unexpected | |
//Typeclass that SHOULD witness that two types are equal by "returning" a Bool type. | |
trait TypeEquality[A, B] { | |
type Out <: Bool | |
def out: Boolean | |
} | |
//Implicit definitions | |
object TypeEquality { | |
implicit def eq[A, B](implicit eq: A =:= B) = new TypeEquality[A, B] { type Out = True; def out = true } | |
implicit def neq[A, B](implicit eq: A =:!= B) = new TypeEquality[A, B] { type Out = False; def out = false } | |
} | |
} | |
object TypeEqualityChecksApp extends App { | |
import TypeEqualityChecks._ | |
val sii = implicitly[TypeEquality[Int, Int]] | |
val sis = implicitly[TypeEquality[Int, String]] | |
//This runs flawlessly | |
println(sii.out) | |
println(sis.out) | |
//val checkTypes = implicitly[False =:= s.Out] // DOES NOT COMPILE | |
//val checkTypes2 = implicitly[True =:= s.Out] // DOES NOT COMPILE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment