Can you give a little more context for what you’re trying to do? I’m not clear 
on the various behaviors you want from the different `timespace` variants.

I’m actually not sure whether the order can be relied on, but I think the 
proposed API might be confusing for users of your function because most people 
assume keyword argument order doesn’t matter. My guess is that with a little 
more info on what you’re trying to accomplish we could help find a more 
idiomatic solution.

-s

> On Nov 12, 2015, at 4:19 PM, MA Laforge <ma.laforge...@gmail.com> wrote:
> 
> Hi users,
> 
> I want to be able to generate ranges using a syntax similar to:
> t1=timespace(tstart = 2e-9, ts=1e-9, tfund=20e-9);
> t11=timespace(fs=1/1e-9, tfund=20e-9, tstart=3e-9);
> t2=timespace(tfund=20e-9, tstart=4e-9, ts=1e-9);
> 
> Why not just use different function names?
> Simply put, I would rather not have to use long, ugly names like (timespace1, 
> timespace2, timespace_tfund, timespace_ts, timespace_fs, ...)
> 
> What is unusual/difficult about this solution:
> linspace uses "number of points" - presumably because specifying both 
> timespan, and timestep might result in non-integer number of points (poorly 
> defined).
> So...  in this case, I want timespace to use the first argument (ignoring 
> tstart) to dictate which algorithm to select.  For example: with 
> timespace(ts=1, tfund=20.5), the sampling period, ts is assumed to be exact, 
> and the fundamental period (tfund) will be adjusted to get an integer number 
> of points.
> It would be preferable to use Juila's dispatch system to dispatch on the 
> keyword names (which it does not - at least not directly).
> Since all arguments here are keywords, Julia will only support a single 
> master function "timespace(; kwargs...)".
> Attempted Solution
> I have a solution, but it depends on keyword ordering... and I don't know if 
> this is a dangerous thing to do: I believe keyword ordering is *not* 
> guaranteed.
> 
> Anyways, here is the solution...
> #Dispatchable keyword type (make name short - expect heavy use)
> immutable KD{Symbol}; end #Not kraft dinner
> 
> #Convert kwargs to a list of dispatchable elements:
> function dispatchable(kwargs::Vector{Any})
>     result = Any[] for (k,v) in kwargs
>         push!(result, KD{k}())
>         push!(result, v)
>     end
>     return result
> end
> 
> timespace() = throw("timespace requires arguments")
> timespace(;tstart=0, kwargs...) =
>     timespace(dispatchable(kwargs)..., tstart=tstart)
> 
> function timespace(::KD{:ts}, ts, ::KD{:tfund}, tfund; tstart=0)
>     println("FUNCTION1 (priority on ts): ts=$ts, tfund=$tfund, 
> tstart=$tstart")
> end
> 
> #Similar to previous, but expects fs = 1/ts
> function timespace(::KD{:fs}, fs, ::KD{:tfund}, tfund; tstart=0)
>     println("FUNCTION1.1 (priority on fs): fs=$fs, tfund=$tfund, 
> tstart=$tstart")
> end
> 
> function timespace(::KD{:tfund}, tfund, ::KD{:ts}, ts; tstart=0)
>     println("FUNCTION2 (priority on tfund): tfund=$tfund, ts=$ts, 
> tstart=$tstart")
> end
> 
> This solution works (for the time being).  The generic timespace(;kwargs...) 
> function catches all the keyword argument, and converts the to normal 
> arguments.  After that, Julia's dispatch system finds the appropriate 
> specialization.
> 
> Problem
> The problem is that I don't know how safe it is to rely on the order of the 
> keyword arguments, as done here.
> 
> Is there maybe an alternative solution to this problem that would still keep 
> the solution/naming succinct and easy to understand?
> 
> (NOTE: this might be better suited for julia-dev, because it has to do with 
> the intended use of the keyword arguments)
> 
> I hope someone out there has an answer, or a better solution to this problem.
> 
> Regards,
> 
> MA

Reply via email to