Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding \mid to the parser and setting its precedence higher than :: as an experimental feature #52756

Open
JanisErdmanis opened this issue Jan 5, 2024 · 1 comment
Labels
parser Language parsing and surface syntax speculative Whether the change will be implemented is speculative unicode Related to unicode characters and encodings

Comments

@JanisErdmanis
Copy link

There have been long discussions on GitHub and recently on Discourse on making an operator like | as syntax sugar for union type, which is driven by recent precedents in python, typescript, and scala. The consensus has come that it needs to be explored in the package first to see if there is any serious demand for this feature. On top of that, it requires punning the meaning of | and preferably changing its precedence over ::, which seems burdening.

Currently, a close sibling, \mid, is unregistered:

julia> ∣
ERROR: ParseError:
# Error @ REPL[1]:1:1
∣
╙ ── unknown unicode character '∣'

This looks like a perfect opportunity to put it in a parser with precedence higher than ::, enabling us to explore its usefulness as a union syntax in a package. The burden that it is a non-ASCII character can be viewed as a benefit as it allows us to find out if the higher precedence for \mid is more useful than | in spite of additional costs. This, perhaps in the future, could help to guide the decision whether Julia 2.0 needs | as a higher precedence operator than ::. A similar analysis can already be done with a macro; however, this is significantly more intrusive and can make other useful macros break when used in combination.

If this feature request is fulfilled, then it would be possible to add the following sugar with a package:

using UnionSyntax: ∣

struct Document
    body::Vector{UInt8}
    signature::Signature∣Nothing
end

This has the benefit that it is easier to add optionality later by putting a cursor in one place vs putting it in two as it is now when it goes from Signature -> Union{Signature, Nothing}.

@stevengj
Copy link
Member

stevengj commented Jan 5, 2024

My feeling is that this is too visually similar to | to parse it as a distinct operator. This came up in #6929, where @JeffBezanson wrote:

I don't know if should be banned out-right, but the confusion with | is certainly enough to keep it out of Base.

(There is also the issue that = U+2223 already has a mathematical meaning assigned by the Unicode standard: "divides".)

@stevengj stevengj added speculative Whether the change will be implemented is speculative unicode Related to unicode characters and encodings parser Language parsing and surface syntax labels Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax speculative Whether the change will be implemented is speculative unicode Related to unicode characters and encodings
Projects
None yet
Development

No branches or pull requests

2 participants