This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Problem: _* type annotation with implicit

7 replies
Peter 2
Joined: 2011-02-16,
User offline. Last seen 42 years 45 weeks ago.

I'm about to extend a factory object (here object My) with a second
signature for apply. It works as expected until I use the type
annotation _*. Here is my narrowed down version:

case class N[A](val n: A)
class My[A]
object My {
def apply[A](a: N[A]*) = new My[A]
def apply[A](s: String)(a: N[A]*) = new My[A]
}
implicit def any2N[A](a: A): N[A] = N(a)
implicit def seq2N[A](s: Seq[A]): Seq[N[A]] = s map (x => N(x))

// the following combinations work
My(1, 2)
My("A")(1, 2)
My("A")(Seq(1, 2): _*)

// but here I get "no ': _*' annotation allowed here"
My(Seq(1, 2): _*)

Is this behavior by design? I wish it worked, too!

bmaso
Joined: 2009-10-04,
User offline. Last seen 2 years 41 weeks ago.
Re: Problem: _* type annotation with implicit
Seq[Int]* can't be passed as an argument to the first overloaded version. Needs to be a Seq[N[_]], like so:

My(Seq(N(1), N(2),):_*)

--
Best regards,
Brian Maso
(949) 395-8551
Follow me: @bmaso
brian [at] blumenfeld-maso [dot] com


On Tue, Jul 5, 2011 at 7:24 AM, Sonnenschein <peter [dot] empen [at] arcor [dot] de> wrote:
I'm about to extend a factory object (here object My) with a second
signature for apply. It works as expected until I use the type
annotation _*. Here is my narrowed down version:

case class N[A](val n: A)
class My[A]
object My {
 def apply[A](a: N[A]*)            = new My[A]
 def apply[A](s: String)(a: N[A]*) = new My[A]
}
implicit def any2N[A](a: A): N[A] = N(a)
implicit def seq2N[A](s: Seq[A]): Seq[N[A]] = s map (x => N(x))

// the following combinations work
My(1, 2)
My("A")(1, 2)
My("A")(Seq(1, 2): _*)

// but here I get "no ': _*' annotation allowed here"
My(Seq(1, 2): _*)

Is this behavior by design? I wish it worked, too!




Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Problem: _* type annotation with implicit
Well, I'm curious now. Why doesn't the implicit conversion work for
My(Seq(1, 2): _*) 

but it does work for 
My("A")(Seq(1, 2): _*) ?

Thanks,Ken
Peter Maas
Joined: 2010-03-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Problem: _* type annotation with implicit
Well both work for me? How is 'My' defined?

-- 
On 5 jul. 2011, at 18:03, Ken McDonald <ykkenmcd [at] gmail [dot] com> wrote:

Well, I'm curious now. Why doesn't the implicit conversion work for
My(Seq(1, 2): _*) 

but it does work for 
My("A")(Seq(1, 2): _*) ?

Thanks,Ken
DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Problem: _* type annotation with implicit

One way to get all calls working and avoid ambiguity at the same time:

case class N[A](val n: A)
class My[A]
object My {
def apply[A](a: N[A]*) = new My[A]
def apply[A](s: String*)(a: N[A]*) = new My[A] // change s to varargs
of strings
}

implicit def any2N[A](a: A): N[A] = N(a)
implicit def seq2N[A](s: Seq[A]): Seq[N[A]] = s map (x => N(x))

My(1, 2)
My("A")(1, 2)
My("A")(Seq(1, 2): _*)

My(Seq(1, 2)) // remove varargs type annotation

Peter 2
Joined: 2011-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Problem: _* type annotation with implicit

So there seems to be no real expanation.

I have narrowed down this case from a collection implementation. "def
apply[A](a: N[A]*)" has ever been working with the application
"My(Seq(1, 2): _*)" - you can test this by commenting out the second
apply. This application is essential and also part of my test suite.

@Dave
>  My(Seq(1, 2))  // remove varargs type annotation

Think of My[A] as List[A], for example. Certainly you can call
List(Seq(1, 2): _*) and List(Seq(1, 2)) but these are semantically
different yielding List[Int] and List[Seq[Int]].

@Peter
> Well both work for me? How is 'My' defined?

How is that possible? I've tried with 2.9.0.final and 2.9.0.1 with no
difference.

@Brian
> Seq[Int]* can't be passed as an argument to the first overloaded version.
Needs to be a Seq[N[_]],

That is the problem, the implicit should come into play but it does
not - here at least.

@Ken
I am curious, too. Unfortunately this is not a puzzle by intent.

@Paul
Sorry I did not find this in the issues database, though I did not
look for closed tickets...

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Problem: _* type annotation with implicit

Taking a second look, I think this is a bug, because these calls below
are working in your original code so type annotation _* should work
too (I don't see why not).

scala> My(Seq(1,2),Seq(3,4))
res0: My[Seq[Int]] = My@a896b8

scala> My(Seq(1,2))
res1: My[Seq[Int]] = My@5a5622

You could report it in Jira. Didn't find this issue there yet. If you
post the link of the issue here I will vote on it.
https://issues.scala-lang.org/secure/Dashboard.jspa

Peter 2
Joined: 2011-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Problem: _* type annotation with implicit

> You could report it in Jira. Didn't find this issue there yet. If you
> post the link of the issue here I will vote on it.https://issues.scala-lang.org/secure/Dashboard.jspa

I have created the Jira Issue https://issues.scala-lang.org/browse/SI-4774.
Thanks!

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland