Originally created by @taichi-ishitani on GitHub (Sep 16, 2024).
Pkl will return the wrong result for the unary - operation like below.
// foo.pkl
a = --1
The result of this operation should be 1 but got -1.
Originally created by @taichi-ishitani on GitHub (Sep 16, 2024).
Pkl will return the wrong result for the unary `-` operation like below.
```pkl
// foo.pkl
a = --1
```
The result of this operation should be `1` but got `-1`.

adam
added the bug label 2025-12-30 01:22:13 +01:00
Thanks for the report; definitely looks like bug. Haven't looked into it yet, but this looks parser related. Unary minus otherwise works as expected:
negativeOne = -1
a = -negativeOne
Produces:
negativeOne = -1
a = 1
@bioball commented on GitHub (Sep 16, 2024):
Thanks for the report; definitely looks like bug. Haven't looked into it yet, but this looks parser related. Unary minus otherwise works as expected:
```pkl
negativeOne = -1
a = -negativeOne
```
Produces:
```
negativeOne = -1
a = 1
```
Double unary minus could be easily confused with a decrement.
Java, JavaScript, Golang, .NET all fail to compile --1 expression either expecting parentheses around (-1) or expecting a variable to be decremented instead of a literal.
Increment/decrement ++ and -- are deprecated in Swift 2.2 and removed in Swift 3.
Python, however, handles x = --1 correctly.
I think pkl should follow the simplest solution and expect ++a or --a to fail with a clear message .
An alternative is to start supporting prefix (and postfix for symmetry?) increments and decrements:
Expect --a to fail when a is numerical literal
Expect --a to mean "decrease a by 1 and return the new value of a as result" when a is a variable
What do you think?
@DFrenkel commented on GitHub (Sep 19, 2024):
Double unary minus could be easily confused with a decrement.
Java, JavaScript, Golang, .NET all fail to compile `--1` expression either expecting parentheses around `(-1)` or expecting a variable to be decremented instead of a literal.
Increment/decrement `++` and `--` are deprecated in Swift 2.2 and removed in Swift 3.
Python, however, handles `x = --1` correctly.
I _think_ pkl should follow the simplest solution and expect `++a` or `--a` to fail with a clear message .
An alternative is to start supporting prefix (_and postfix for symmetry?_) increments and decrements:
- Expect `--a` to fail when `a` is numerical literal
- Expect `--a` to mean "decrease a by 1 and return the new value of a as result" when `a` is a variable
What do you think?
A decrement operator of any kind seems like the wrong choice for Pkl as it implies some form of imperative mutation that doesn't exist in the language.
@HT154 commented on GitHub (Sep 21, 2024):
This bug extends through parentheses as well:
```
x = -(-1)
// x = -1
```
A decrement operator of any kind seems like the wrong choice for Pkl as it implies some form of imperative mutation that doesn't exist in the language.
Good point, @DFrenkel. I'm sympathetic to the idea that --a should hard-fail, because it might be confused with decrement.
@bioball commented on GitHub (Oct 11, 2024):
Good point, @DFrenkel. I'm sympathetic to the idea that `--a` should hard-fail, because it might be confused with decrement.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @taichi-ishitani on GitHub (Sep 16, 2024).
Pkl will return the wrong result for the unary
-operation like below.The result of this operation should be
1but got-1.@bioball commented on GitHub (Sep 16, 2024):
Thanks for the report; definitely looks like bug. Haven't looked into it yet, but this looks parser related. Unary minus otherwise works as expected:
Produces:
@madrob commented on GitHub (Sep 16, 2024):
This is fun!
@DFrenkel commented on GitHub (Sep 19, 2024):
Double unary minus could be easily confused with a decrement.
Java, JavaScript, Golang, .NET all fail to compile
--1expression either expecting parentheses around(-1)or expecting a variable to be decremented instead of a literal.Increment/decrement
++and--are deprecated in Swift 2.2 and removed in Swift 3.Python, however, handles
x = --1correctly.I think pkl should follow the simplest solution and expect
++aor--ato fail with a clear message .An alternative is to start supporting prefix (and postfix for symmetry?) increments and decrements:
--ato fail whenais numerical literal--ato mean "decrease a by 1 and return the new value of a as result" whenais a variableWhat do you think?
@HT154 commented on GitHub (Sep 21, 2024):
This bug extends through parentheses as well:
A decrement operator of any kind seems like the wrong choice for Pkl as it implies some form of imperative mutation that doesn't exist in the language.
@bioball commented on GitHub (Oct 11, 2024):
Good point, @DFrenkel. I'm sympathetic to the idea that
--ashould hard-fail, because it might be confused with decrement.