I would like to be able to write 1--2
to get 3
. Additional spaces or parenthesis are not convenient.
Matlab and Python allow this. JavaScript not, because there ++
and --
are increment/decrement operators, but -+
and +-
are ok.
julia> 1++2
3
julia> 1-+2
-1
julia> 1+-2
-1
julia> 1--2
ERROR: -- not defined
julia> 1- -2
3
julia> 1-(-2)
3
I hope you're not expecting anyone else to read code that has 1--2
in it, I'd almost call that an enforced-readability feature if not for 1++2
currently working.
I would be surprised to find 1--2 == 3
. Adding space makes it much more readable.
Why is this a bug? --
is a free operator symbol that can be defined by the user, for example even by -- = +
if that is really wanted.
@mschauer agreed, but it does seem inconsistent that ++
is not parsed as a unique operator:
julia> parse("1++2")
:(1 + 2)
Here's what we'll do:
--
and ++
as operators.a++b
parsing as a + (+b)
to avoid spurious breakage.--
a syntax error while we decide on (1). Note **
is already a syntax error to avoid confusion with ^
.-1 on having --
and ++
be valid operators.
I once proposed that ++x
and --x
could default to x += one(x)
and x -= one(x)
, but allow more efficient implementations (e.g. for BigInts), the main advantage being that it avoids the type instability that can be introduced by x += 1
, e.g. for UInt32
on a 64-bit system. I'm not sure how important that is, however, since it doesn't seem to come up that often.
It would be nice to have pred
and succ
functions. It would be a bit odd to have syntax for those functions in combination with assignment, but not the functions themselves.
I ran into this yesterday, where in the UnitRange
constructor pred(start)
would be better than start-one(stop-start))
.
I don't think there is anything left to do here. Deciding on --
and ++
as operators seems like a separate issue.