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

Variable length argument list as a call by name

4 replies
Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.

Is there a way to do someting like this :

def f[A](cbn: =>(A*)) = ...

?


If my memory is not inventing things, there was a solution posted
a while ago involving an implicit conversion, but I can't find it,
(if it ever existed ;-))

Thanks
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Variable length argument list as a call by name
Don't know, but did you try this?

def f[A](cbn: CBN[A]*) = ...
case class CBN[A](f: ()=>A)
implicit def cbn2CBN[A](a: =>A) = CBN(()=>a)


On Tuesday, December 13, 2011, Maxime Lévesque <maxime.levesque@gmail.com> wrote:
>
> Is there a way to do someting like this :
>
> def f[A](cbn: =>(A*)) = ...
>
> ?
>
>
> If my memory is not inventing things, there was a solution posted
> a while ago involving an implicit conversion, but I can't find it,
> (if it ever existed ;-))
>
> Thanks
>
barden
Joined: 2010-03-30,
User offline. Last seen 33 weeks 5 days ago.
unpacking tuple dislikes variables starting with capitals
Hi all,   is there a reasoning behind or it this a compiler error (scala 2.9.0):   scala> val e = (1.0, 2.0)
e: (Double, Double) = (1.0,2.0)   scala> val (x, y) = e
x: Double = 1.0
y: Double = 2.0   scala> val (X, Y) = e
<console>:8: error: not found: value X
       val (X, Y) = e
            ^
<console>:8: error: not found: value Y
       val (X, Y) = e   Regards, Volker.
From: scala-user@googlegroups.com [mailto:scala-user@googlegroups.com] On Behalf Of Naftoli Gugenheim
Sent: 16 December 2011 04:21
To: Maxime Lévesque
Cc: scala-user@googlegroups.com
Subject: Re: [scala-user] Variable length argument list as a call by name

Don't know, but did you try this?

def f[A](cbn: CBN[A]*) = ...
case class CBN[A](f: ()=>A)
implicit def cbn2CBN[A](a: =>A) = CBN(()=>a)


On Tuesday, December 13, 2011, Maxime Lévesque <maxime.levesque@gmail.com> wrote:
>
> Is there a way to do someting like this :
>
> def f[A](cbn: =>(A*)) = ...
>
> ?
>
>
> If my memory is not inventing things, there was a solution posted
> a while ago involving an implicit conversion, but I can't find it,
> (if it ever existed ;-))
>
> Thanks
>
Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: unpacking tuple dislikes variables starting with capitals

> is there a reasoning behind or it this a compiler error (scala 2.9.0):

It is not a compiler error, because this assignment is actually somewhat
similar to pattern matching [1].

> scala> val (x, y) = e
> x: Double = 1.0
> y: Double = 2.0

That's basically like saying

e match {
case (x, y) => ...
}

> scala> val (X, Y) = e
> :8: error: not found: value X
> val (X, Y) = e
> ^
> :8: error: not found: value Y
> val (X, Y) = e

Same here. Unescaped upper case identifiers in patterns refer to
existing variables (i. e. extractors).

[1] Some time ago I thought it would be the same, but after some
discussions on the list it turned out that there are quite a few corner
cases. If you are brave, read the full story there:

barden
Joined: 2010-03-30,
User offline. Last seen 33 weeks 5 days ago.
RE: Re: unpacking tuple dislikes variables starting with capita

Thanks Lars, thanks Rex,

Hm, I wonder why I never hooked this up. I can't remember that one of the text books pointed to this, but very likely I didn't look hard enough.

Cheers, Volker.

-----Original Message-----
From: scala-user@googlegroups.com [mailto:scala-user@googlegroups.com] On Behalf Of Lars Hupel
Sent: 18 December 2011 20:14
To: scala-user@googlegroups.com
Subject: [scala-user] Re: unpacking tuple dislikes variables starting with capitals

> is there a reasoning behind or it this a compiler error (scala 2.9.0):

It is not a compiler error, because this assignment is actually somewhat similar to pattern matching [1].

> scala> val (x, y) = e
> x: Double = 1.0
> y: Double = 2.0

That's basically like saying

e match {
case (x, y) => ...
}

> scala> val (X, Y) = e
> :8: error: not found: value X
> val (X, Y) = e
> ^
> :8: error: not found: value Y
> val (X, Y) = e

Same here. Unescaped upper case identifiers in patterns refer to existing variables (i. e. extractors).

[1] Some time ago I thought it would be the same, but after some discussions on the list it turned out that there are quite a few corner cases. If you are brave, read the full story there:

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