Thanks Matt, that helps my understanding of keyword arguments.

It also occurs to me that if you don't really require *keyword* arguments 
then you could define as:

function g(s::AbstractString)
    println(s)
end

function g{S<:AbstractString}(s::AbstractString, v::Vector{S})
    println(s)
    for e in v
        println(e)
    end
end

and call as:

julia> g("boo")
boo

julia> g("bla", ["foo", "bar"])
bla
foo
bar



On Saturday, March 26, 2016 at 10:43:17 AM UTC+11, Matt Bauman wrote:

> There's a few things going on here.  First, you only define one method for 
> `g` — your second definition overwrites the first because keyword arguments 
> don't participate in multiple dispatch.  There's been a warning added here 
> in 0.5.
>
> So the one method you define just takes one argument and one keyword 
> argument.  That's why `g("bla", ["foo", "bar"])` isn't working.  If you 
> instead call `g("bla", v=["foo", "bar"])`, you'll get a stack overflow 
> error with the second definition, and a type assertion failure with the 
> first.  Instead of converting your `v` argument to a 
> `Vector{AbstractArray}`, you can just use the first definition with a type 
> parameter.
>
> Finally, the `S not defined` error is a known surprising behavior: 
> https://github.com/JuliaLang/julia/issues/15613.  When there's no keyword 
> argument given, how can Julia know what `S` should be?
>
> On Friday, March 25, 2016 at 7:15:09 PM UTC-4, Dominique Orban wrote:
>>
>> I (think I) understand the issue in this thread: 
>> https://groups.google.com/forum/#!searchin/julia-users/AbstractString/julia-users/HBMDbnRTH9g/cuqa6hB8AQAJ
>> but I have the same problem with a keyword argument:
>>
>> function g(s :: AbstractString; v :: Vector{AbstractString}=
>> AbstractString[])
>>    println(s)
>>    for e in v
>>        println(v)
>>    end
>> end
>>
>>
>> I need to dispatch on the optional argument `v`, so:
>>
>> g{S <: AbstractString}(s :: AbstractString; v :: Vector{S}=Vector{S}[]) = 
>> g(s, v=convert(Vector{AbstractString}, v))
>>
>>
>>
>> but unfortunately:
>>
>> julia> g("bla", ["foo", "bar"])
>> ERROR: MethodError: `g` has no method matching g(::ASCIIString, ::Array{
>> ASCIIString,1})
>> Closest candidates are:
>>   g(::AbstractString)
>>
>>
>> Moreover,
>>
>> julia> g("boo")
>> ERROR: UndefVarError: S not defined
>>
>>
>> Any ideas?
>>
>

Reply via email to