Qalculate / libqalculate

Qalculate! library and CLI
https://qalculate.github.io/
GNU General Public License v2.0
1.74k stars 143 forks source link

[feature request] Variables as references to other variables + better multi-line support #622

Open pwspen opened 5 months ago

pwspen commented 5 months ago

Qalculate is a really powerful tool and I've greatly enjoyed using it so far. I'm an engineer that does a lot of sizing calculations for various projects and automatic unit checking in such a light package is so useful.

I have two feature requests:

  1. Variables re-evaluated when variable/function referencing them is ran. Current behavior:

    a = 1
    b = 2
    c = a*b // 2
    a = 3
    c // still 2

    I'm proposing that c would be 6, or at least to add a mode where things work like that. This would probably involve changes to the save() function that saves any equation referencing other variables as an equation instead of a calculated result.

  2. Related, I'd like better multi-line support in the GUI. I'd like to be able to run something like the above and not have it break. It seems like currently the GUI is very prone to interpreting variable names as units where the CLI does not, and I'm not sure the state of intended multi-line support.

I know nothing about the codebase right now but I'm a decent programmer so I can help implement these if they wouldn't mess everything up. I'm also interested in adding Python bindings (#605).

hanna-kn commented 5 months ago

Use := (e.g. c := a*b // 2), which sets the 3rd argument of save() to 1, to avoid evaluation of the expression of the variable. (Alternatively the variable command or the new/edit variable dialog in the GUIs can be used.)

Related, I'd like better multi-line support in the GUI. I'd like to be able to run something like the above and not have it break. It seems like currently the GUI is very prone to interpreting variable names as units where the CLI does not, and I'm not sure the state of intended multi-line support.

I'm not sure what you mean with multi-line support. There is no difference in how the GUIs interprets expressions, nor in how variables and units are handled. the above should return the exact same result, unless you enter all expressions together without pressing Enter, or clicking =, in between.

pwspen commented 5 months ago

Didn't know about :=, awesome!

Being able to enter all the expressions together and only hit enter once is exactly what I'm asking for. For many of my usecases, it would be very convenient to have 10-20 lines that specify variables and calculations etc and be able to edit things and evaluate the final result without hitting enter on each line individually.

hanna-kn commented 4 months ago

Variable assignment can be performed in a single expression, on one or multiple lines, using where (or /.), with assignments separated by comma or "and".

a + b + c where
a = 1,
b = 2,
c = a*b // 2

Confusingly a + b + c is displayed are + barn + SpeedOfLight (without affecting the result), and assigning values containing other variables defined in the same expression (c = a*b // 2), does not work properly in version 4.9.0. This will be fixed in the next version (it has been fixed in Git).

Interpreting individual lines as separate expressions conflicts with the ability to input single expressions with line breaks. It should however be possible to implement an alternative syntax for where statements, with variable assignments separated by comma (with option line break) placed before the expression (e.g. a = 1, b = 2, c = a*b // 2, a + b + c).