Julia: -- parses as an operator but ++ doesn't

Created on 1 Jan 2015  路  9Comments  路  Source: JuliaLang/julia

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
decision parser

All 9 comments

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:

  1. Decide whether we want -- and ++ as operators.
  2. For now keep a++b parsing as a + (+b) to avoid spurious breakage.
  3. Make -- 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yurivish picture yurivish  路  3Comments

musm picture musm  路  3Comments

i-apellaniz picture i-apellaniz  路  3Comments

ararslan picture ararslan  路  3Comments

omus picture omus  路  3Comments