Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Assignment = expressiveness #11

Open
WilliamRagstad opened this issue Jan 31, 2022 · 1 comment
Open

Assignment = expressiveness #11

WilliamRagstad opened this issue Jan 31, 2022 · 1 comment
Labels
↻ breaking Suggestions or fixes that are breaking changes in some way. • enhancement New feature or request ∆ feature New feature suggestion # prioritize Issues that are more urgent than others

Comments

@WilliamRagstad
Copy link
Member

WilliamRagstad commented Jan 31, 2022

So assignments in languages throughout history have always been implemented as statements. But in the recent functional era of languages code have gotten more and more expressive based. Though assignment expressions returned their value for allowing code like below is nice.

a = b = c = 0 # Multi assignment

I got the idea to fully combine assignments with pattern matching, making the expression return true or false weather the "matching" was successful. This way we could also write code like this

a = 2 * 2 + 1
b = 10 / 2
if (a = b) ...

and only use one operator for both assignment and comparison.
The = operator could therefore be seen as an infix operator evaluated right-to-left and updating the environment after matching every term in the expression, thus fulfilling the transitive property of equality. This would allow for code as show in the first example, AND in the latter.

Then one problem arises. What if we would like a variable to obtain the boolean value of if two other variables were the same.
Because = would be an boolean infix operator with side-effects. Wrapping an expression in parenthesis would force that expression to evaluate first and then return true or false if the matching was successful, meaning the terms were equal or one variable were undefined and therefore defined with the value of the other terms value (true), or the expression was not equal (false).
So, this code would be valid:

a = 7
b = 42
c = (a = b) # false

Linked to #12.

@WilliamRagstad WilliamRagstad added the • enhancement New feature or request label Jan 31, 2022
@WilliamRagstad
Copy link
Member Author

WilliamRagstad commented Jan 31, 2022

And also, if a and b are undefined, the code below would be valid as well:

(a = 7) = (b = 42)
#  T    =    T
#       T

But for non-transitive single-line assignments, the code below would still be desired.

a = 7; b = 42

@WilliamRagstad WilliamRagstad added # prioritize Issues that are more urgent than others ∆ feature New feature suggestion ↻ breaking Suggestions or fixes that are breaking changes in some way. labels Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
↻ breaking Suggestions or fixes that are breaking changes in some way. • enhancement New feature or request ∆ feature New feature suggestion # prioritize Issues that are more urgent than others
Projects
None yet
Development

No branches or pull requests

1 participant