- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Question about constructor with multiple argument lists
Wed, 2010-01-13, 01:19
I understand (and very much appreciate) function definitions with multiple
parameter lists like:
def fun(a : Int)(b : String) = a.toString + b
as opposed to
def gun(a : Int,b : String) = a.toString + b
allows fun to be partially applied as in foo(fun(3)).
It appears we can also have
class hun(a : Int)(b : String)
but it seems (to me, that is) there is no way to partially apply the
constructor for hun in any way. Is that correct and if so: why would I want
to define a class this way (apart from the syntactical difference between
new hun(1)("2") and new hun(1,"2"))?
Thanks,
Silvio
Wed, 2010-01-13, 01:57
#2
Re: Question about constructor with multiple argument lists
Daniel Sobral wrote:
> I don't know if it really isn't possible, or if this is just a bug. But
> there are benefits to doing it this way:
>
> 1. Types inferred from the first argument list can be used to infer the
> second argument list. This is explained on Programming in Scala, by Odersky
> et al.
>
> 2. Values received in the first argument list can be used as defaults in the
> second argument list.
>
> I don't know if they actually work with curried classes as well, but I don't
> see why they wouldn't.
>
> On Tue, Jan 12, 2010 at 10:18 PM, Silvio Bierman <
> sbierman@jambo-software.com> wrote:
>
>
>> I understand (and very much appreciate) function definitions with multiple
>> parameter lists like:
>>
>> def fun(a : Int)(b : String) = a.toString + b
>>
>> as opposed to
>>
>> def gun(a : Int,b : String) = a.toString + b
>>
>> allows fun to be partially applied as in foo(fun(3)).
>>
>> It appears we can also have
>>
>> class hun(a : Int)(b : String)
>>
>> but it seems (to me, that is) there is no way to partially apply the
>> constructor for hun in any way. Is that correct and if so: why would I want
>> to define a class this way (apart from the syntactical difference between
>> new hun(1)("2") and new hun(1,"2"))?
>>
>> Thanks,
>>
>> Silvio
>>
>> --
>> View this message in context:
>> http://old.nabble.com/-scala-user--Question-about-constructor-with-multi...
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>>
>
>
>
There is also the possibility of using an implicit constructor argument.
Wed, 2010-01-13, 02:07
#3
Re: Question about constructor with multiple argument lists
On Tue, Jan 12, 2010 at 04:18:40PM -0800, Silvio Bierman wrote:
> It appears we can also have
>
> class hun(a : Int)(b : String)
>
> but it seems (to me, that is) there is no way to partially apply the
> constructor for hun in any way.
scala> class hun(a : Int)(b : String)
defined class hun
scala> new hun(5)(_)
res0: (String) => hun =
scala> res0("abc")
res1: hun = hun@1e26fbfc
Wed, 2010-01-13, 02:17
#4
Re: Question about constructor with multiple argument lists
Thanks Daniel and Tony for pointing that out. I might even have come up with
these but was looking for something in the direction of currying.
Paul Phillips-3 wrote:
>
> scala> class hun(a : Int)(b : String)
> defined class hun
>
> scala> new hun(5)(_)
> res0: (String) => hun =
>
> scala> res0("abc")
> res1: hun = hun@1e26fbfc
> ...
>
Thank you Paul, this was exactly what I was looking for. I just expected the
more intuitive
new hun(5) //which I tried, of course
to result in String => hun
instead of new hun(5)(_)
Anybody who knows the deeper rationale behind disallowing plain new hun(5)
which is more in line with the syntax for partially applied functions?
Cheers,
Silvio
1. Types inferred from the first argument list can be used to infer the second argument list. This is explained on Programming in Scala, by Odersky et al.
2. Values received in the first argument list can be used as defaults in the second argument list.
I don't know if they actually work with curried classes as well, but I don't see why they wouldn't.
On Tue, Jan 12, 2010 at 10:18 PM, Silvio Bierman <sbierman@jambo-software.com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.