Parser support for open/closed interval notation?

I’m working with Interval{DateTime} these days, in particular, within a medical data processing application that requires modeling of with closed/open endpoints. It’s inconvenient to write/update intervals due to the permutations of open/closed on each of the right/left endpoints. The Intervals.jl library is using more traditional syntax for show(), for example, [1..3) indicates that the left-most endpoint is closed and the right-most is open. On the other hand, IntervalSets.jl eschews this notation, and uses 1..3 (closed-open) for its representation of the same interval. I’m wondering if this important use case have specialized syntax support in the parser? That is, when you see .. permit ] to close a ( and permit ] to close a (. With this parser change, it might be easier to support custom syntaxes via macros that handle open/closed variants, and also unify what seems to be an unnecessary notation divergence between these two Interval implementations.

I don’t think that extending Julia’s syntax is the best solution here, since the resulting AST is otherwise invalid, and would need to keep track of the parentheses itself, so it is a huge change for a very specialized use case.

Instead, I would recommend a non-standard literal, eg iv"[1..3)" could just parse to the relevant constructor (allowing the endpoints to be expressions, of course).

2 Likes