Created
July 8, 2014 03:11
-
-
Save bvenners/ee4b19d20564e1429786 to your computer and use it in GitHub Desktop.
Is there any way to get the Scala compiler to infer that TYPECLASS1 is The[String]#Equaling in dude(egual("hi"))
This file contains hidden or 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
organization := "com.example" | |
version := "0.1" | |
scalaVersion := "2.11.1" | |
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") | |
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.0" | |
initialCommands in console := """ | |
import org.scalatest.matchers.{MatcherFactory1, MatchResult, Matcher} | |
import org.scalactic.{Constraint,Prettifier} | |
import org.scalactic.TypeCheckedTripleEquals._ | |
trait The[R] { | |
type Equaling[L] = Constraint[L, R] | |
} | |
def egual[R](right: R): MatcherFactory1[Any, The[R]#Equaling] = | |
new MatcherFactory1[Any, The[R]#Equaling] { | |
def matcher[T <: Any : The[R]#Equaling]: Matcher[T] = { | |
val equalityConstraint = implicitly[Constraint[T, R]] | |
new Matcher[T] { | |
def apply(left: T): MatchResult = { | |
val (leftee, rightee) = (left, right) | |
MatchResult( | |
equalityConstraint.areEqual(left, right), | |
left + "did not equal" + right, | |
left + "equaled" + right, | |
Vector(leftee, rightee), | |
Vector(left, right) | |
) | |
} | |
override def toString: String = "equal (" + Prettifier.default(right) + ")" | |
} | |
} | |
override def toString: String = "equal (" + Prettifier.default(right) + ")" | |
} | |
def dude[TYPECLASS1[_]](rmf1: MatcherFactory1[String,TYPECLASS1])(implicit typeClass1: TYPECLASS1[String]) = "howdy" | |
egual("hi") | |
dude[The[String]#Equaling](egual("hi")) | |
""" |
This file contains hidden or 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
$ sbt | |
> console | |
[info] Updating {file:/Users/bv/nobkp/delus/typeLambdaInference/}typelambdainference... | |
[info] Resolving jline#jline;2.11 ... | |
[info] Done updating. | |
[info] Starting scala interpreter... | |
[info] | |
warning: there were 1 feature warning(s); re-run with -feature for details | |
import org.scalatest.matchers.{MatcherFactory1, MatchResult, Matcher} | |
import org.scalactic.{Constraint, Prettifier} | |
import org.scalactic.TypeCheckedTripleEquals._ | |
defined trait The | |
egual: [R](right: R)org.scalatest.matchers.MatcherFactory1[Any,[L]org.scalactic.Constraint[L,R]] | |
dude: [TYPECLASS1[_]](rmf1: org.scalatest.matchers.MatcherFactory1[String,TYPECLASS1])(implicit typeClass1: TYPECLASS1[String])String | |
res0: String = howdy | |
Welcome to Scala version 2.11.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> dude(egual("hi")) | |
<console>:16: error: no type parameters for method dude: (rmf1: org.scalatest.matchers.MatcherFactory1[String,TYPECLASS1])(implicit typeClass1: TYPECLASS1[String])String exist so that it can be applied to arguments (org.scalatest.matchers.MatcherFactory1[Any,[L]org.scalactic.Constraint[L,String]]) | |
--- because --- | |
argument expression's type is not compatible with formal parameter type; | |
found : org.scalatest.matchers.MatcherFactory1[Any,[L]org.scalactic.Constraint[L,String]] | |
required: org.scalatest.matchers.MatcherFactory1[String,?TYPECLASS1] | |
dude(egual("hi")) | |
^ | |
<console>:16: error: type mismatch; | |
found : org.scalatest.matchers.MatcherFactory1[Any,[L]org.scalactic.Constraint[L,String]] | |
required: org.scalatest.matchers.MatcherFactory1[String,TYPECLASS1] | |
dude(egual("hi")) | |
^ | |
<console>:16: error: could not find implicit value for parameter typeClass1: TYPECLASS1[String] | |
dude(egual("hi")) | |
^ | |
scala> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment