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
:= 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
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.
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
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
Edited: three diff interpo: cmd too; futhermore the verbatim strings can may be blocked from reuse there. No interpo in md"…$foo…" AFAIK
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