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

Question about constructor with multiple argument lists

4 replies
Silvio Bierman
Joined: 2009-02-16,
User offline. Last seen 1 year 16 weeks ago.

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

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Question about constructor with multiple argument lists
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 [at] jambo-software [dot] 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-multiple-argument-lists-tp27137517p27137517.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Daniel C. Sobral

I travel to the future all the time.
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
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 [at] jambo-software [dot] 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.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
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

Silvio Bierman
Joined: 2009-02-16,
User offline. Last seen 1 year 16 weeks ago.
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

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