Wrong result of unary - operation #207

Closed
opened 2025-12-30 01:22:13 +01:00 by adam · 5 comments
Owner

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.

image

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`. ![image](https://github.com/user-attachments/assets/4e2583eb-db52-4756-80ac-b1de87ec5fe2)
adam added the bug label 2025-12-30 01:22:13 +01:00
adam closed this issue 2025-12-30 01:22:13 +01:00
Author
Owner

@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:

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 ```
Author
Owner

@madrob commented on GitHub (Sep 16, 2024):

This is fun!

@madrob commented on GitHub (Sep 16, 2024): This is fun!
Author
Owner

@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?

@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?
Author
Owner

@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.

@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.
Author
Owner

@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.

@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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#207