Until we get "diagonal dispatch," I think the only way to do this is to expand
the number of arguments in the function:
myfunc(vec::Vector) = _myfunc(eltype(vec), vec) # internal function
_myfunc{D<:Dict{ASCIIString}}(::Type{D}, vec) = 1
_myfunc{D}(::Type{D}, vec) = 2
julia> myfunc([Dict("a"=>1), Dict("b"=>1.0)])
1
julia> myfunc([Dict(1=>:hello), Dict(2=>:world)])
2
Best,
--Tim
On Thursday, July 14, 2016 1:05:20 PM CDT Yichao Yu wrote:
> On Thu, Jul 14, 2016 at 12:41 PM, David Barton <[email protected]> wrote:
> > Hi,
> >
> > I'm trying to write a function that accepts a vector of values that are of
> > the same (overall) type but with different parameterisations. As a simple
> > example, consider the vector = [Dict("a"=>1), Dict("b"=>1.0)]. I can
> > easily
> > use a function along the lines of
> >
> > function myfunc(vec::Vector{Dict})
> >
> > # do something
> >
> > end
> >
> > but I'd like to be able to restrict the parameterisation slightly so that
> > the first parameter of the Dict type is an ASCIIString (not my actual use
> > case but follows exactly the same pattern). I've tried doing something
> > like
> >
> > function myfunc{T}(vec::Vector{Dict{ASCIIString, T}})
> >
> > # do something
> >
> > end
> >
> > but this seems to enforce the condition that all the Dicts in the vector
> > have the same parametric type T (so my example of myfunc([Dict("a"=>1),
> > Dict("b"=>1.0)]) fails). Is there any way of expressing this constraint?
> > Or
>
> The issue is the type of `[Dict("a"=>1), Dict("b"=>1.0)]` It has type
> `Vector{Dict{String}}` which cannot be matched by the signature you
> provide. I'm not sure if there's currently a clean way to be able to
> match this type in additional to the more strict types.
>
> > will I just have to use the first form of myfunc with some extra checking
> > in the function body?
> >
> > Thanks
> > David