Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setvar out = out or mydict idiom doesn't work with empty dict, maybe deepcopy() #1866

Open
andychu opened this issue Mar 16, 2024 · 3 comments

Comments

@andychu
Copy link
Contributor

andychu commented Mar 16, 2024

          Ohh yess, Hmmmm  

Sorry setvar out = out or mydict has a bit of a pitfall -- when out is the empty dict.

The or operator tests if a value is "falsey", and that includes empty containers -- {} [] false null

This is a bit of an unfortunate interaction -- it's something Python also has. I actually tried to change what the or operator meant, but it causes other problems elsewhere in the language.


So really the right thing for dicts is

if (param === null) {
   setvar param = defaults
}

rather than

setvar param = param or defaults

Originally posted by @andychu in #1842 (comment)

@andychu
Copy link
Contributor Author

andychu commented Mar 16, 2024

Another possibility is to just evaluate at call time

That's probably easiest

However there is still the possibility of aliasing a global there ...

Like

proc p (d={k: globaldict}, mylist=[globaldict]) {
}

@bar-g
Copy link
Contributor

bar-g commented Mar 16, 2024

However there is still the possibility of aliasing a global there ...

I think, the possibility is good, very much needed actually, to keep and update an external state, by default, and be able to call the same proc to process with another mutable for particular runs.

It's only the misleading "=" assignment meaning reference semantics in this case, that makes it implement the python-pitfall.

A well integrated ->-syntax feature, and defining that external default as mylist->[globaldict] should be able to solve this pitfall as well, and allow for mylist=[globaldict] to make a deepcopy. (Can that be done without a full traversal with what you named "pickling"?)

@andychu
Copy link
Contributor Author

andychu commented Mar 25, 2024

FWIW JavaScript now has syntactic sugar for null only checks (not falsey)


    foo ?? bar is syntactic sugar for ((foo === undefined) or (foo === null)) ? bar : foo.
    foo?.bar is syntactic sugar for ((foo === undefined) or (foo === null)) ? undefined : foo.bar.

https://www.scattered-thoughts.net/writing/unexplanations-sql-is-syntactic-sugar-for-relational-algebra/

Someone asked for ?. and destructuring on Zulip I think too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants