Skip to content

Commit

Permalink
Merge pull request #53 from travisbrown/topic/relax-type-eq-constraint
Browse files Browse the repository at this point in the history
Relax type equality constraint to subtype
  • Loading branch information
mpilquist committed Jan 30, 2016
2 parents ae1e204 + 815b7a2 commit c2416db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/scala/simulacrum/typeclass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class TypeClassMacros(val c: Context) {
if (arg equalsStructure Ident(simpleArg)) {
(withRewrittenFirst, true)
} else {
val typeEqualityType = tq"_root_.scala.Predef.=:=[${liftedTypeArg.name}, $arg]"
val typeEqualityType = tq"_root_.scala.Predef.<:<[${liftedTypeArg.name}, $arg]"
val equalityEvidence = ValDef(Modifiers(Flag.IMPLICIT), TermName(c.freshName("ev")), typeEqualityType, EmptyTree)
val updatedParamss = {
if (withRewrittenFirst.nonEmpty && withRewrittenFirst.last.head.mods.hasFlag(Flag.IMPLICIT))
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/scala/simulacrum/typeclass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,22 @@ class TypeClassTest extends WordSpec with Matchers {
import Foo.ops._
Option(Option(1)).bar
}

"supports type inference for syntax for operations where the instance type is constrained" in {
@typeclass trait Ap[F[_]] {
def ap[A, B](ff: F[A => B])(fa: F[A]): F[B]
}

object Ap {
implicit val apOption: Ap[Option] = new Ap[Option] {
def ap[A, B](ff: Option[A => B])(fa: Option[A]): Option[B] = ff.flatMap(fa.map)
}
}

import Ap.ops._
val ff: Option[Int => String] = Some(_.toString)
ff.ap(Some(0)) shouldBe Some("0")
}
}

"support annotated companions" in {
Expand Down

0 comments on commit c2416db

Please sign in to comment.