Skip to content

Navigation Menu

Sign in
Appearance settings
Sign up
Appearance settings

OEP8a1: The object() function

Jordan Brown edited this page Jul 7, 2025 · 3 revisions

This proposal includes the object() and has_key() functions from OEP8.

  • Objects (dictionaries? structures? associative arrays?)

Credits

Revar Desmera did the original implementation of object().

Objects (dictionaries? structures? associative arrays?)

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.

Background

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.name or in an array-style form obj[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() and str() 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() function

  • 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 of o with a replaced.

An array-style entry with no value deletes the name from the accumulating object:

  • object(..., [[name_expr], ...], ...)

thus

  • object(o, [["a"]]) yields a copy of o without a.

has_key() function

has_key(obj, key) returns true if key is present in obj.

Open questions

object() needed?

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?

Name of has_key() function

This name leaves me a bit cold. Maybe contains(), especially if it is polymorphic in some sense?

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.