Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zipped does not work for arrays #2673

Closed
scabug opened this issue Nov 20, 2009 · 6 comments
Closed

zipped does not work for arrays #2673

scabug opened this issue Nov 20, 2009 · 6 comments
Assignees

Comments

@scabug
Copy link

scabug commented Nov 20, 2009

Over lists I can do:

scala> val xs= List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)

scala> (xs, xs).zipped map (_ + _)
res3: List[Int] = List(2, 4, 6)

But the same over arrays gives:

scala> val xs = Array(1, 2, 3)
xs: Array[Int] = Array(1, 2, 3)

scala> (xs, xs).zipped map (_ + _)
<console>:6: error: could not find implicit value for parameter w1: <:<[Array[Int],scala.collection.TraversableLike[El1,Repr1]]
       (xs, xs).zipped map (_ + _)
                ^
@scabug
Copy link
Author

scabug commented Nov 20, 2009

Imported From: https://issues.scala-lang.org/browse/SI-2673?orig=1
Reporter: @odersky

@scabug
Copy link
Author

scabug commented Nov 20, 2009

@adriaanm said:
This is because zip(ped) uses <:< instead of <%<, so I assume I can go ahead and implement the TODO in there?

BTW, I guess I shouldn't be editing TupleN.scala, but updating genprod instead...

// TODO: probably loosen zip and zipped from <:< to <%<

  def zip[Repr1, El1, El2, To](implicit w1:   T1 <:< TraversableLike[El1, Repr1], 
                                        w2:   T2 <:< Iterable[El2], 
....

@scabug
Copy link
Author

scabug commented Nov 25, 2009

@adriaanm said:
It turns out changing <:< to <%< does not fix this.

as I understand it, we currently do implicit search with type vars as type args and then solve the type vars (including the constraints the implicit search imposed), however it seems only "top-level" implicits may include typevars, the type params of transitively required implicits must be determined immediately

is this accurate? could/should we change this to simply further accumulate constraints and solve at the top-level?

in the meantime, I'll commit a quick fix to zipped (just use => instead of <%<)

@scabug
Copy link
Author

scabug commented Nov 25, 2009

@adriaanm said:
to make this more concrete, consider this (edited!) debugging output:

typing implicit with undetermined type params: List(type Repr1, type El1, type Repr2, type El2)  
tree: Test.this.MyTuple2.apply[Array[Int], Array[Int]](Test.this.as, Test.this.as).zipped[Repr1, El1, Repr2, El2]

typed impl?? conformsOrViewsAs:[A,B](implicit evidence$$1: Function1[A,B]) Predef.<%<[A,B] 
   ==> Predef.<%<.conformsOrViewsAs 
   with pt = Predef.<%<[Array[Int],collection.this.TraversableLike[El1,Repr1]], 
     wildpt = Predef.<%<[Array[Int],collection.this.TraversableLike[?,?]]

typed implicit Predef.<%<.conformsOrViewsAs:[A, B](implicit evidence$$1: Function1[A,B]) Predef.<%<[A,B], 
   pt = Predef.<%<[Array[Int], collection.this.TraversableLike[?,?]]

typing implicit with undetermined type params: List(type B)
tree: Predef.<%<.conformsOrViewsAs[Array[Int], B]

I think it would be better if the undetermined type param B in the last two lines would track the constraint B <: Collection.this.TraversableLike[?El1, ?Repr1] (where these are type variables introduced when typing the top-level implicit)

test code (+predef is modified to move <:< and conforms to LowestPriorityImplicits):

import scala.collection.{TraversableLike, IterableLike}

object Test {
  case class MyTuple2[+T1, +T2](_1:T1, _2:T2) {
    def zipped[Repr1, El1, Repr2, El2](implicit w1: T1 <%< TraversableLike[El1, Repr1], w2: T2 <%< IterableLike[El2, Repr2]): Zipped[Repr1, El1, Repr2, El2] = new Zipped[Repr1, El1, Repr2, El2](_1, _2)
    class Zipped[+Repr1, +El1, +Repr2, +El2](coll1: TraversableLike[El1, Repr1], coll2: IterableLike[El2, Repr2])
  }

  val as = Array(1, 2, 3)
  MyTuple2(as, as) zipped
}

@scabug
Copy link
Author

scabug commented Nov 25, 2009

@adriaanm said:
quick fix is in r19861

@scabug
Copy link
Author

scabug commented Nov 25, 2009

@retronym said:
After we discussed <%< on the list recently, I had trouble convincing myself it provided value above the incumbent =>. Were we were just sucked in by the elegance of {=:=, <:<, <%<}?

In this case, it just prevents the caller to zipped explicitly passing w1, w2. What am I missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants