The .. operator doesn’t support chaining like that.
One way to define custom constructor syntax is to use a string macro, which lets you parse expressions in any way that you want (completely independent of standard Julia syntax). For example:
julia> macro date_str(s)
args = Meta.parse.(split(s, '-'))
:(Date($args...))
end
@date_str (macro with 1 method)
julia> date"2022-4-20"
2022-04-20
(Though I’m not convinced that this particular example is much clearer than just typing Date(2022, 4, 20), it certainly seems a lot clearer than 2022..4..20.)
PS. What you are describing is not a unary operator — it is a binary operator .. that you want to chain without using parentheses. I’m not sure why the .. doesn’t support chaining, but many other custom operators do, e.g.
First, while .. does not support chainning, so I have change it to ~; Second, It is also confusing that when calling 2022~1~1, It calls ~(1,1) firstly, then calls ~(2022,(1,1)). I thought it would be backward.
Thanks for your help. ++ support any number of arguments, it would be the reason way ++ works while .. does not. I also find another way to do it with not having to use ++:
It’s cool (and very surprising to me, possible), but not what I would want people to start using (for Dates nor ~). I’m not sure why, but unlike with ++, the example with 2022~1~1 didn’t work for me (in Julia 1.7.0 or 1.9):
julia> Base.:(~)(year_num::Int,i::I{Int})=Date(year_num,i.x,i.y)
ERROR: UndefVarError: I not defined
At least the latter is chaining, and whatever you want to call with ++ (chaining or) ternary operator, are there such ternary operator uses, in any Julia code base? For any operator (or just in math, at least very rare).