printfn / fend

Arbitrary-precision unit-aware calculator
https://printfn.github.io/fend
MIT License
634 stars 51 forks source link

Support assertions #229

Open twolodzko opened 1 year ago

twolodzko commented 1 year ago

It would be great to have assertions, so it would be possible to do

2 + 2 == 4  # returns TRUE
2 + 2 == 5  # returns FALSE
2 + 2 != 5   # returns TRUE

This could be helpful for verifying calculations, e.g. expression == my_answer to validate manual calculations.

A more controversial syntax (but arguably, closer to actual math syntax) might overload the = operator, so

x = 4        # x was not used before, so it is an assignment
x = 2 + 2  # assertion is true, nothing happens
x = 2 + 3  # Error: x is not equal to 5
y = 8 / 2   # y was not used before, assign
x = y        # get x and y and assert to compare for equality

So the above would work in a similar way on how = operator works in Erlang or Elixir, see https://learnyousomeerlang.com/starting-out-for-real#invariable-variables

Having the above would also help to get rid of accidental overwrites like

pi = 42

If forced assignment would be needed, there's the := symbol commonly used in math for it.

printfn commented 1 year ago

Thanks for the suggestion! Assertions (and comparison operators in general) are definitely on my radar to add at some point.