Are `:=`, `$=` operators still supported?

Are :=, $= operators still in use today ? If yes is there any examples of their usage.
I can get a MWE for both of them

.


:= OPERATOR

A = rand(0:100,2,5)
A[:,1:2] = 1:4 # works ok

A := 1:10 # will throws a syntax ERROR
A[:,1:2] := (1:4...,) # will throws a syntax ERROR; may never leave the frontend

:= is marked as “;; unsupported assignment operators” at the end the frontend

;; https://github.com/JuliaLang/julia/blob/v1.6.2/src/julia-syntax.scm#L4341
;; unsupported assignment operators
((≔ ⩴ ≕ :=)
    (error (string "unsupported assignment operator \"" (deparse (car e)) "\"")))

but still parsed at its beginning

;; https://github.com/JuliaLang/julia/blob/v1.6.2/src/julia-parser.scm#L110
; operators that are special forms, not function names
(define syntactic-operators
  (append! (add-dots '(= += -= *= /= //= |\\=| ^= ÷= %= <<= >>= >>>= |\|=| &= ⊻=))
           '(:= $= && |\|\|| |.| ... ->)))

without any (direct) occurences detected.

.


$= OPERATOR

is still handled as an assignment op

   '$=     lower-update-op

it may be used indirectly, thru assignment-op processing, but hum, a MWE is not so obvious to me

I’m very open for suggestions there

:= and friends are deliberately supported only in the parser, so they can be used inside macros in the context of DSLs for example. It might get some special meaning at some point in the future. There have been various proposals in the past, but so far there hasn’t been any definite decision or someone stepping forward and implementing it.

$= is just the updating version of $, which behaves like any other binary operator, e.g. you could write the following:

julia> x $ y = xor(x, y)
$ (generic function with 1 method)

julia> x = 0x01
0x01

julia> x $= 0x03
0x02

julia> x
0x02
7 Likes

OK so they are some kind of “empty placeholder” ops, one has to fill them with user code before using them.

That may sound like a bit strange handshaking between core/user code.

F# allows customs operators thru operator overloading. you build what you want from allowed op chars.

Creating New Operators

You can overload all the standard operators, but you can also create new operators out of sequences of certain characters. Allowed operator characters are !, $, %, &, *, +, -, ., /, <, =, >, ?, @, ^, |, and ~. The ~ character has the special meaning of making an operator unary, and is not part of the operator character sequence. Not all operators can be made unary.

Operator Overloading - F# | Microsoft Learn

With a few special named operators recognized by the parser and lowered asap (next section, “Overloaded Operator Names”). Like julia, but with a cleaner boundary IMHO

Haskell too has its own with custom fixity eg. precedence.
That may lead to impedance if redef occurs while coding dsl (not at all an haskell specialist)

Anyway, thanks for the quick and precise response.

$= is just the updating version of $ , which behaves like any other binary operator, e.g. you could write the following:

Little addendum :

$ is so much associated with interpolation thru macro, and regex. I don’t know if it will be aliased so much ?! In those two contexts, $ is used as an unary op, while your example def it as a binary one.

I was able to define an unary variant

($)(x::Int) = 2x

But each custom unary call leads to a syntax error

$ 3 # syntax error
($ 3) # syntax error
$3 # syntax error
$(3) # syntax error

all of them work with + for example

0 $ 3 leads to a method error your example will solve.

So binary variant $ escape out from the front end,
but how can one call a custom unary $ please (without metaprogramming )?

You can’t. Unary $ does interpolation, so it’s a syntactic operator that can’t be user overloaded.

OK. Since there are at least two differents interpo, the expr and the string (not the regex, sorry). Julia may have make it more multi-dispatchable as an unary op. But let’s admit the composite case is covered with expr and the litteral one is enough with string.

EX_OR/2 pops here and there in the frontend, the only other ref i have found about it was not at matlab, mathematica, latex, or apl but ahdl. There must be some very good fan of it somewhere but not so far to embed it here so deeply :slight_smile:

Edited: three diff interpo: cmd too; futhermore the verbatim strings can may be blocked from reuse there. No interpo in md"…$foo…" AFAIK

in this case the $ will also need to be in parenthesis:

julia> ($)(x::Int) = 2x
$ (generic function with 1 method)

julia> ($)(3)
6

it’s the same for : that otherwise interprets it as an Expr

julia> :(1,2)
:((1, 2))

julia> (:)(1,2)
1:2
3 Likes

Thanks !


BTW the reservation approach still puzzles me
In v1.6

  • square root is recognized and there
  • cube root is recognized and there
  • quad root is recognized and reserved (yaya square root it twice by yourself, but import chain, type piracy hazard on the way)

Too often, a gate to an half-done job IMHO

The topic is not that old to not be revived recooked
This point does not deserve a whole issue
This is more about $ rather than $=
Pinging is evil

@simeonschaub A last question about $ it’s syntaxic own good karma, using it as an unary op, reserving it as a binary op, and baking the whole perfectly

Consider


let f=:foo, u=:foo_fld ,
    g=:bar, v=:bar_fld ;
    :($f(a) = a.$u
    $g(a) = a.$v)
end

IMHO, it does not generate code as expected.
eg. as

let f=:foo, u=:foo_fld ,
    g=:bar, v=:bar_fld ;
    :($f(a) = a.$u ;
    $g(a) = a.$v)
end

does it.

$ @ $g is interpreted as a binary op between line 3 and 4 rather than two interpolation at line 3 and 4. a.($u) or (a.$u) do not fix it

Is it by design ?
Is it a bug ?
I am inclined to believe that may be even a BDFL may have not pursue that goal

It may not deserve an answer
Happy new week, anyway