Skip to content

Allow ::: as an operator? #58608

@iuliadmtru

Description

@iuliadmtru
Contributor

a:::b is valid Julia syntax:

julia> Meta.show_sexpr(Meta.parse("a:::b"))
(:(::), :a, (:quote, #QuoteNode
    :b
  ))

but is not semantically valid code.

Could ::: be parsed as an operator instead? Like this:

julia> Meta.show_sexpr(Meta.parse("a:::b"))
(:(:::), :a, :b)

Activity

StefanKarpinski

StefanKarpinski commented on Jun 3, 2025

@StefanKarpinski
SponsorMember

Seems good to me. Very technically breaking as a:::b does already parse, but it parses as (a)::(:b) which is useless, so very doubtful this would actually break anything.

added
parserLanguage parsing and surface syntax
triageThis should be discussed on a triage call
on Jun 3, 2025
adienes

adienes commented on Jun 3, 2025

@adienes
Member

time to wake up, a new equality operator just dropped

quinnj

quinnj commented on Jun 3, 2025

@quinnj
Member

We've talked about having x as T syntax before which would do convert(T, x), that was my immediate though seeing this syntax. i.e. we'd have x::T for assertion and x:::T for conversion. But maybe those are too visually similar to be helpful when perusing code 🤷

StefanKarpinski

StefanKarpinski commented on Jun 4, 2025

@StefanKarpinski
SponsorMember

The proposal isn't to give it a definition, just to parse it as an operator. But yes, that's s possibility.

LilithHafner

LilithHafner commented on Jul 3, 2025

@LilithHafner
Member

If we do want to give it a definition and export it from Base we should ideally do that in the same release as we make it an operator; lest packages give it conflicting meanings.

JeffBezanson

JeffBezanson commented on Jul 3, 2025

@JeffBezanson
SponsorMember

@iuliadmtru Do you have an example in mind of how you might use it? That would be helpful to assess the value of this.

DilumAluthge

DilumAluthge commented on Jul 3, 2025

@DilumAluthge
Member

We've talked about having x as T syntax before which would do convert(T, x), that was my immediate though seeing this syntax. i.e. we'd have x::T for assertion and x:::T for conversion. But maybe those are too visually similar to be helpful when perusing code 🤷

For this specific example, I think that x::T and x:::T are too visually similar and would be confusing.

iuliadmtru

iuliadmtru commented on Jul 3, 2025

@iuliadmtru
ContributorAuthor

@iuliadmtru Do you have an example in mind of how you might use it? That would be helpful to assess the value of this.

I have been using it for a syntax matching mechanism in Argus.jl to represent pattern variables restricted by syntax classes:

@pattern {x:::identifier} = {_:::expr}

This is a pattern that matches an assignment. x and _ are pattern variables (the latter is anonymous) and identifier and expr are syntax classes. I wanted something that is different from :: but close enough to it to suggest similar semantics.

LilithHafner

LilithHafner commented on Jul 3, 2025

@LilithHafner
Member

Triage is fine with this if someone wants to implement and it's not too much complexity. We'd just make it an operator with some precedence, we wouldn't define a meaning.

martinholters

martinholters commented on Jul 4, 2025

@martinholters
Member

A language-level use-case for ::: I could imagine is like a stricter ::, such that x:::T ensures that typeof(x) == T or similar. This could make sense for dispatch, like so:

julia> f(x::T, y::Vector{T}) where {T} = T # normal case
f (generic function with 1 method)

julia> f(1, [])
Any

julia> g(x:::T, y::Vector{T}) where {T} = T # T == typeof(x)
g (generic function with 1 method)

julia> g(1, []) # fail as typeof(1) != eltype([])
ERROR: MethodError: no method matching g(::Int64, ::Vector{Any})

julia> g(1, Int[])
Int

Not sure how useful this would be and whether it would be imaginable to fit this into the subtyping/dispatch mechanism in the foreseeable future, but I wanted to float the idea before we make the syntax available and regret later on.

removed
triageThis should be discussed on a triage call
on Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserLanguage parsing and surface syntax

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @StefanKarpinski@JeffBezanson@quinnj@DilumAluthge@martinholters

        Issue actions

          Allow `:::` as an operator? · Issue #58608 · JuliaLang/julia