Skip to content

Commit

Permalink
Constrain tuple syntax with IsTuple
Browse files Browse the repository at this point in the history
  * Resolves ambiguity with product syntax
  * Allows to correctly generalize (including unit)
  • Loading branch information
joroKr21 committed Mar 18, 2020
1 parent 0495f42 commit 5891711
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
9 changes: 2 additions & 7 deletions core/src/main/scala/shapeless/syntax/std/tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ package std

import shapeless.ops.hlist.ProductToHList

trait LowPriorityTuple {
implicit def productTupleOps[P <: Product](p: P): TupleOps[P] = new TupleOps(p)
}

object tuple extends LowPriorityTuple {
implicit def unitTupleOps(u: Unit): TupleOps[Unit] = new TupleOps(u)

object tuple {
// Duplicated here from shapeless.HList so that explicit imports of tuple._ don't
// clobber the conversion to HListOps.
implicit def hlistOps[L <: HList](l : L) : HListOps[L] = new HListOps(l)
implicit def productTupleOps[P: IsTuple](p: P): TupleOps[P] = new TupleOps(p)
}

final class TupleOps[T](t: T) extends Serializable {
Expand Down
13 changes: 10 additions & 3 deletions core/src/test/scala/shapeless/tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package shapeless

import org.junit.Test
import org.junit.Assert._

import shapeless.ops.tuple.IsComposite
import shapeless.test._
import testutil._

Expand Down Expand Up @@ -49,6 +49,7 @@ class TupleTests {
case class Pear() extends Fruit
case class Banana() extends Fruit

case class Foo(i: Int, s: String)
type PWS = Product with Serializable with Fruit

type YYYY = (Any, Any, Any, Any)
Expand Down Expand Up @@ -1462,8 +1463,7 @@ class TupleTests {

@Test
def testPropagation: Unit = {
def useHead[P <: Product](p: P)(implicit ic: ops.tuple.IsComposite[P]) = p.head

def useHead[P: IsTuple: IsComposite](p: P) = p.head
val h = useHead((23, "foo", true))
typed[Int](h)
}
Expand Down Expand Up @@ -1973,4 +1973,11 @@ class TupleTests {
(23, "foo").align[(String, String)]
""")
}

@Test
def testCompatibilityWithProductSyntax: Unit = {
import syntax.std.product._
assertEquals(List(2, "a"), Foo(2, "a").to[List])
assertEquals(List(2, "a"), (2, "a").to[List])
}
}

0 comments on commit 5891711

Please sign in to comment.