Open StefanKarpinski opened 5 years ago
io = open(args...; flags) # not sure about calling syntax here, to splat or not to splat...
I think that would have to be a splat.
I think that would have to be a splat.
Yes, makes sense. Would we be able to specialize the splatting code on the type and avoid the dynamic overhead since we know what the fields of flags
are? Hopefully.
It's not uncommon to want to share a set of keyword arguments across a large number of function signatures. The repetition of the signature is quite annoying; you can use splatting to pass keywords through to a deeper function but that's not idea since you want to check arguments as soon as possible, not deep in the call stack at a function boundary that may not mean much to the user. If everything doesn't get inlined that can also lead to very inefficient splatting and slurping code being generated. At the same time, it's often quite convenient to use a struct type to capture and pass around a bunch of keyword options.
Combining these two trains of thought led me to consider a feature like this, illustrated with an example taken from the
open
function, which has a fairly fancy set of keyword arguments that it can take which are translated into a structure of boolean flag values. See:https://github.com/JuliaLang/julia/blob/1f2b16fd964aa03ccd648c3/base/iostream.jl#L214-L291
Instead, we would express this code as follows:
That just defines an immutable structure for flags to open and similar functions. On the using side you would declare a function like this:
In other words, having
(flags...)::OpenFlags
means that any keyword arguments to this open method are given to theOpenFlags
constructor instead in order to construct an instance which is then available within the function asflags
. Later where we have:https://github.com/JuliaLang/julia/blob/1f2b16fd964aa03ccd648c/base/iostream.jl#L366-L373
We could instead write this instead of slurping and splatting keyword args through a try/catch:
As an added benefit, this could provide a single centralized place to handle deprecation of keyword arguments: just deprecate a keyword argument in the constructor of the appropriate options structure and every place that uses it will get the deprecation automatically, supporting the old keyword with the appropriate warning.