Skip to content

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

Open
@JanisErdmanis

Description

@JanisErdmanis

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}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserLanguage parsing and surface syntaxspeculativeWhether the change will be implemented is speculativeunicodeRelated to unicode characters and encodings

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions