Recommended 0.6 API for Dict-like constructors and functions

question

#1

Suppose I have a function (usually, but not necessarily a constructor) with signature myfun(xs...) and I want it to be able to take a generator, eg myfun(something(x) for x in itr). An example of this style in Base is Dict.

What is the recommended way of writing this in 0.6? The cheap way out is

myfun(xs::Base.Generator) = myfun(xs...)

but I don’t think that is optimal. Looking at the source of Base.Dict(kv), I see that first it tries associative_with_eltype and then fails with a nice error message if that is not applicable. So is Base.associative_with_eltype the way to go if I want similar functionality?


#2

Splatting is definitely not a good solution since it means the method needs to be recompiled for each number of elements in xs. I don’t know the specifics of your use case, but it would probably be better to define methods the other way around, with:

myfun(xs...) = myfun(xs)

The comparison with Dict doesn’t completely apply since it needs a generator of Pair. Unless that’s also your case, your requirements should be less strict: generally, you just need to be able to iterate over itr. In that case, mean is a better example.