Jim Balter | 23 Feb 04:54 2011

Re: Default parameters with currying

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
<naftoligug@...> 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 <Jim@...> 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()
>> <console>: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 <vpatryshev@...>
>> wrote:
>>>
>>> I believe print2(5)() would work...
>>>
>>> 2011/2/21 ssanj <sanjsmailbox@...>
>>>>
>>>> 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)
>>>> <console>: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
>>
>
>


Gmane