-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
OEP8a: object literals and object comprehensions #6081
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
base: master
Are you sure you want to change the base?
Conversation
|
You really need all the seemingly esoteric options? The all can be done in other ways and they will require a lot of real estate in the cheat sheet? Maybe I misread the idea of OpenSCAD but I understood it was to keep the language as simple as possible. Also, by using expr for the name you have a lot of error cases to handle or do you want to handle non-string keys? For simplicity and consistency, I'd follow the object() syntax we now have as close as possible to prevent a lot of confusion and keep the cheat sheet small? If you think it is important to have ':' as separator for JS compatibility then lets introduce a ':' operator that appends? This would match very well with ranges as well as the syntax of object(). I think matches is the syntax of range already? Just an idea. |
The object comprehensions (for, if, let) are all intended to match the existing list comprehensions. Over the course of the discussion (mostly a couple of years ago) one of the biggest questions seemed to be whether we needed But yes, everything that you can do with object comprehensions you can do with
Right now I want to prohibit non-string keys, with the intent that they might eventually be allowed. I wouldn't be surprised if there are currently bug-oids where non-strings are converted to strings. But many of the interesting applications require dynamically constructed keys, both for creating objects and accessing them.
Note that object() does not have a syntax. It is just a function, with standard function syntax.
This suggests that to create an object in the simplest case you have to put quotes around all of your names:
Plausible.
In my quick summary above, I used a full if...else, but you are also allowed to conditionally include a value by just saying
Paralleling list comprehension. Putting the "let" outside the object literal widens its scope, which may be undesirable in complex constructions. You can also use the let() operator inside an expression, but that narrows the scope undesirably; it can't wrap around a for...if comprehension.
Again, paralleling list comprehension. In list comprehension, you need "each" because otherwise there is syntactically no way to distinguish "pull apart this list and add its members individually" from "add this list as an element". Object comprehension doesn't have that particular requirement, because an expression doesn't syntactically match "name: value". Note that for both list comprehension and object comprehension, "each" is equivalent to a "for" loop that iterates across the source list/object.
Being like JS and Python - this has similarities to both JS object syntax and Python dictionary syntax - is certainly good, but the major reason that it uses
I don't think that can work with ?:.
Ranges have brackets around them. Actually, your suggestion there would conflict with ranges: is |
|
A slight simplification would be to eliminate the However, I think this syntax is the same as JSON, and JSON always puts quotes around its names. In fact, this syntax is close enough to JSON that we should probably ensure that it is JSON-compatible, that a block of JSON data would be directly usable as OpenSCAD input, and that the result of an echo() or str() in OpenSCAD is valid JSON data. |
Devil is in the details :-) Personally I am always easy to convince for any additional functionality but I also see that the drive to keep the cheat sheet small and not create another Perl with 10 ways to do the same thing has a lot of merit? One issue, will you be able to use the results of a prior field like in |
|
I would say that we add complex argument semantics on top of object, without adding any syntax. But I agree that's a quibble.
Is I'm not sure without asking Bison's opinion. But I'm pretty sure that the range thing is a conflict.
Not at present. They are not variables. That's consistent with JS and Python. (Just as when constructing a list you cannot refer to previous elements... though with a list there's no obvious way to even try to refer to them.) |
Neither am I but I am used to parsers where you can set the priority. I asked ChaGPT and she says yes ... not that I always trust her. However, I am not fighting for this feature, seems nice but not a big deal.
But not with OpenSCAD's |
|
To clarify a little: it's consistent with Python dictionary syntax. Python objects don't have a distinct syntax; they are populated through normal statement execution and there you can refer to previous assignments. That's not really an option for us, since objects (like all other OpenSCAD values) are immutable and so you can't add new entries to an existing object. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
(Since you still have |
[ Not ready for merge ]
Implements the remaining "object" features from OEP8, as extracted in OEP8a.
This adds object literals in a JavaScript-like syntax, including object comprehensions.
For a more complete writeup see OEP8a, but briefly:
{ name: value, ... }- object with constant identifier-safe names{ "name": value, ... }- object with constant names not limited to identifiers{ (expr): value, ... }- object with names from expressions{ for (...) (name): value, ... }- generate several members{ if (...) name: value else name: value, ... }- conditionally generate members{ let(...) ... }- set variables and evaluate object comprehension{ each object }- split up and add each of its membersThis PR is not ready for merge. I wrote it a couple of years ago as part of PR#4478, and extracted it. I haven't reviewed it yet, but others are welcome to take a look.