-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
OEP8a1: The object() function
This proposal includes the object() and has_key() functions from OEP8.
- Objects (dictionaries? structures? associative arrays?)
Revar Desmera did the original implementation of object().
The idea is to add a function that creates data values equivalent to what JavaScript calls objects, Python calls dictionaries, and some other languages call structures or associative arrays.
Current development-snapshot OpenSCAD has some support for an object data type, in support of the textmetrics, fontmetrics, and import(json) functions:
- An object has any number of member elements, each of which is of any data type, and each of which has a unique name.
- Objects are, like all other OpenSCAD data items, immutable.
- A member element is identified by its name, either in a constant identifier form
obj.nameor in an array-style formobj[expr]. The identifier form allows only identifier-legal characters; the array-style form allows any characters. The lookup is case-sensitive. - An object can be traversed using
for;for (name=obj)yields the names of the object's member elements. -
is_object(expr)returns true if the expression evaluates to an object. -
echo()andstr()yield a printable representation of an object, but it does not correspond to legal syntax. - The current snapshot does not include any way for an OpenSCAD program to create an object; objects are created only as return values from the textmetrics, fontmetrics, and import functions.
-
object(name=expr, ...)- Constructs an object with the specified elements.
-
object([[name_expr, val_expr], ...], ...)- Constructs an object with the specified elements. Note that the names can be expressions, and that the name-value list can be the result of arbitrary list construction.
-
object(o1, o2, ...)- Constructs an object with the elements from the objects passed.
These can be combined arbitrarily. If a particular name appears more than once, the last definition is used, so
-
object(o, a="new")makes a copy ofowithareplaced.
An array-style entry with no value deletes the name from the accumulating object:
object(..., [[name_expr], ...], ...)
thus
-
object(o, [["a"]])yields a copy ofowithouta.
has_key(obj, key) returns true if key is present in obj.
Is the object() function desirable, given object comprehension from OEP8? Is it technically needed? Even if it is not technically needed, is it a useful alternative way to look at the problem?
This name leaves me a bit cold. Maybe contains(), especially if it is polymorphic in some sense?