Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Jim Balter <Jim-dBgi35m8Gtt7tPAFqOLdPg <at> public.gmane.org>
Subject: Re: Default parameters with currying
Newsgroups: gmane.comp.lang.scala.user
Date: Wednesday 23rd February 2011 03:54:54 UTC (over 5 years ago)
It could be a subclass of Function1 that has both unary and nullary
apply methods, the latter of which calls the former with the default,
and the compiler could invoke the appropriate one depending on the
syntax -- Scala is, after all, a strongly statically typed language.

-- Jim

On Tue, Feb 22, 2011 at 6:59 PM, Naftoli Gugenheim
 wrote:
> Default arguments are a feature of methods, not functions.
> How might default arguments for functions be implemented?
>
>
> On Tue, Feb 22, 2011 at 2:56 AM, Jim Balter
<[email protected]> wrote:
>>
>> Unfortunately,
>>
>> scala> def print2(n:Int)(s:String = "blah") = println(n+s)
>> print2: (n: Int)(s: String)Unit
>>
>> scala> def p5 = print2(5) _
>> p5: (String) => Unit
>>
>> scala> p5("a")
>> 5a
>>
>> scala> p5()
>> :8: error: not enough arguments for method apply: (v1:
>> String)Unit in trait Function1.
>> Unspecified value parameter v1.
>>        p5()
>>          ^
>>
>> does not work, but it sure would be nice if the partial application
>> inherited the default argument.
>>
>> -- Jim
>>
>> On Mon, Feb 21, 2011 at 11:28 PM, Vlad Patryshev

>> wrote:
>>>
>>> I believe print2(5)() would work...
>>>
>>> 2011/2/21 ssanj 
>>>>
>>>> Hi Guys,
>>>>
>>>> I'm having a small problem with currying and default parameters.
>>>>
>>>> I can define a function as:
>>>>
>>>> def print(n:Int, s:String = "blah") {}
>>>> print: (n: Int,s: String)Unit
>>>>
>>>> I can call it with:
>>>>
>>>> print(5)
>>>> print(5, "testing")
>>>>
>>>> If I curry the above:
>>>>
>>>> def print2(n:Int)(s:String = "blah") {}
>>>> print2: (n: Int)(s: String)Unit
>>>>
>>>> I can't call it with 1 parameter:
>>>>
>>>> print2(5)
>>>> :7: error: missing arguments for method print2 in object $iw;
>>>> follow this method with `_' if you want to treat it as a partially
>>>> applied function
>>>>       print2(5)
>>>>
>>>> I have to supply both parameters. Is there some way around this?
>>>
>>>
>>> --
>>> Thanks,
>>> -Vlad
>>
>
>
 
CD: 24ms