you could probably come up with better error messages, but the following is
valid syntax:

*julia> **f(;a=error("a is required"),b=error("b is required")) = (a,b)*

*f (generic function with 1 method)*


*julia> **f(a=1)*

*ERROR: b is required*


*julia> **f(a=1,b=4)*

*(1,4)*


*julia> **f()*

*ERROR: a is required*


*julia> *


On Sun, Nov 2, 2014 at 3:46 PM, Kevin Owens <[email protected]> wrote:

> Is there any way to do it without setting default values? Or, is there a
> type you can give to default values so that if you try to use them it will
> throw an error? I'm thinking like with R and the "NA" value.
>
> Do keyword arguments have to have default values? It's not clear in the
> 0.3 documentation on functions that it's the case.
>
>
>
> On Sunday, November 2, 2014 12:47:26 PM UTC-6, Jack Minardi wrote:
>>
>> You need to give default values for each keyword argument:
>>
>> type Foo
>>     bar
>>     baz
>>     Foo(;bar=10, baz=1) = new(bar, baz)
>> end
>>
>>
>> On Sunday, November 2, 2014 1:05:37 PM UTC-5, Kevin Owens wrote:
>>>
>>> I'm using Julia 0.3 something.
>>>
>>> If I make a composite type with many fields I may forget the order, but
>>> remember their names. I'd like to use a constructor where I can use name
>>> the arguments.
>>>
>>> Say I have the composite type
>>>
>>> type Foo
>>>
>>>   bar
>>>
>>>   baz
>>>
>>> end
>>>
>>>
>>> How can I make a constructor that lets me do this
>>>
>>> myfoo = Foo(baz=1, bar=2)
>>>
>>> I expected this would work
>>>
>>>
>>> type Foo
>>>
>>>   bar
>>>
>>>   baz
>>>
>>>
>>>   Foo(;bar, baz) = new(bar, baz)
>>>
>>> end
>>>
>>>
>>> But when I run it I get
>>>
>>> ErrorException("syntax: invalid keyword argument bar")
>>>
>>>
>>> I also tried
>>>
>>>
>>>
>>> julia> function Foo(;bar, baz)
>>>
>>>
>>>     Foo(bar, baz)
>>>
>>>
>>> end
>>>
>>>
>>>
>>>
>>> ErrorException("syntax: invalid keyword argument bar")
>>>
>>>
>>>
>>>

Reply via email to