- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Variable length argument list as a call by name
Tue, 2011-12-13, 19:35
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
Sun, 2011-12-18, 17:51
#2
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
>
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
>
Sun, 2011-12-18, 20:21
#3
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:
Mon, 2011-12-19, 00:11
#4
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:
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
>