Assuming I have some thing like this:
julia> struct AA val end julia> struct Composed members config end
Is there any acceptable way to enable something like this:
julia> Base.:+(a::AA...;config="default") = Composed(collect(a), config) julia> AA(1) + AA(2) + AA(3) Composed(AA[AA(1), AA(2), AA(3)], "default")
while still allowing to set the
config attribute to something else than default? For example:
AA(1) + AA(2) + AA(3) <magic op> config = "new"
"new" <magic op> AA(1) + AA(2) + AA(3)
The motivation is only to provide syntatic sugar. It might also give some context that the creation of the real Composed is a bit messy even though it is straight forward (which is why the sugar is desirable).
The extra parameters are config-type parameters so one possible solution is to have a global config variable or an overloadable method (I guess this is basically the same) which returns the wanted config, but this has the obvious drawbacks of global state.
One option could be to just give a curry function and let users define their own composition op should they want to:
julia> compose_from(config) = (a...) -> Composed(collect(a), config) compose_from (generic function with 1 method) julia> + = compose_from("test") #13 (generic function with 1 method) julia> AA(1) + AA(2) + AA(3) Composed(AA[AA(1), AA(2), AA(3)], "test")
Could metaprogramming do something nicer here? I’m very inexperienced with it so to me it just looks like it would do the curry solution above but using strings.