discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

New feature in 2025.07.11: the object() function

JB
Jordan Brown
Fri, Aug 8, 2025 7:54 AM

On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote:

Workaround: len([for(x=obj)x])

Yeah, I thought about that, but if there's a need for a "workaround"
then we should just make it work.

On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote: > Workaround: `len([for(x=obj)x])` Yeah, I thought about that, but if there's a need for a "workaround" then we should just make it work.
RD
Revar Desmera
Fri, Aug 8, 2025 7:58 AM

I'm not sure when I'de ever actually check how many keys are in an object, except to see if no keys are in the object, in which case I'd just test for !object.
But I don't see why not to support it.

  • Revar

On Aug 8, 2025, at 12:54 AM, Jordan Brown openscad@jordan.maileater.net wrote:

On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote:

Workaround: len([for(x=obj)x])

Yeah, I thought about that, but if there's a need for a "workaround" then we should just make it work.

I'm not sure when I'de ever actually check how many keys are in an object, except to see if no keys are in the object, in which case I'd just test for `!object`. But I don't see why not to support it. - Revar > On Aug 8, 2025, at 12:54 AM, Jordan Brown <openscad@jordan.maileater.net> wrote: > > On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote: >> Workaround: `len([for(x=obj)x])` > > Yeah, I thought about that, but if there's a need for a "workaround" then we should just make it work. > >
JB
Jordan Brown
Fri, Aug 8, 2025 7:59 AM

On 8/7/2025 11:33 PM, Steve Lelievre via Discuss wrote:

Revar’s suggestion  returning the length of a vector of one object,
which isn’t quite the same thing as the length of the object per se.

First, you need to define the length of an object.  Revar's definition,
and the one that I would think of would be "the number of elements in
the object", along the same lines as len(vector). I'm not immediately
coming up with a different definition that would be more useful.  (Heck,
I'm not immediately coming up with a different definition that would be
useful at all.)

For comparison, what does len do for any other non-vector, non-string
variable? (I’m away from home so can’t check)

It's an error.

On 8/7/2025 11:33 PM, Steve Lelievre via Discuss wrote: > Revar’s suggestion  returning the length of a vector of one object, > which isn’t quite the same thing as the length of the object per se. First, you need to define the length of an object.  Revar's definition, and the one that I would think of would be "the number of elements in the object", along the same lines as len(vector). I'm not immediately coming up with a different definition that would be more useful.  (Heck, I'm not immediately coming up with a different definition that would be useful at all.) > For comparison, what does len do for any other non-vector, non-string > variable? (I’m away from home so can’t check) It's an error.
JB
Jordan Brown
Fri, Aug 8, 2025 8:15 AM

On 8/8/2025 12:58 AM, Revar Desmera via Discuss wrote:

I'm not sure when I'de ever actually check how many keys are in an object, except to see if no keys are in the object, in which case I'd just test for !object.
But I don't see why not to support it.

I'm in pretty much the same boat there, except that it's pretty rare
that I even want to know whether there are no keys.

On 8/8/2025 12:58 AM, Revar Desmera via Discuss wrote: > I'm not sure when I'de ever actually check how many keys are in an object, except to see if no keys are in the object, in which case I'd just test for `!object`. > But I don't see why not to support it. I'm in pretty much the same boat there, except that it's pretty rare that I even want to know whether there are no keys.
PK
Peter Kriens
Fri, Aug 8, 2025 8:21 AM

I created a PR.

https://github.com/openscad/openscad/pull/6080

On 8 Aug 2025, at 09:54, Jordan Brown via Discuss discuss@lists.openscad.org wrote:

On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote:

Workaround: len([for(x=obj)x])

Yeah, I thought about that, but if there's a need for a "workaround" then we should just make it work.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I created a PR. https://github.com/openscad/openscad/pull/6080 > On 8 Aug 2025, at 09:54, Jordan Brown via Discuss <discuss@lists.openscad.org> wrote: > > On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote: >> Workaround: `len([for(x=obj)x])` > > Yeah, I thought about that, but if there's a need for a "workaround" then we should just make it work. > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
SL
Steve Lelievre
Fri, Aug 8, 2025 9:25 AM

To me an object’s key count is conceptually different to length so perhaps
instead of monkeying with the behaviour of len to get the key count, a new
built-in function that returns a vector of the keys. Then we could do
things like

echo(len(keys(myObj));

but also process them sequentially

for(i = keys(myObj)) … something …myObj[i];

Kills two birds with one stone.

Steve

On Fri, 8 Aug 2025 at 04:22, Peter Kriens via Discuss <
discuss@lists.openscad.org> wrote:

I created a PR.

https://github.com/openscad/openscad/pull/6080

On 8 Aug 2025, at 09:54, Jordan Brown via Discuss <
discuss@lists.openscad.org> wrote:

On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote:

Workaround: len([for(x=obj)x])

Yeah, I thought about that, but if there's a need for a "workaround" then
we should just make it work.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

To me an object’s key count is conceptually different to length so perhaps instead of monkeying with the behaviour of len to get the key count, a new built-in function that returns a vector of the keys. Then we could do things like echo(len(keys(myObj)); but also process them sequentially for(i = keys(myObj)) … something …myObj[i]; Kills two birds with one stone. Steve On Fri, 8 Aug 2025 at 04:22, Peter Kriens via Discuss < discuss@lists.openscad.org> wrote: > I created a PR. > > https://github.com/openscad/openscad/pull/6080 > > > > On 8 Aug 2025, at 09:54, Jordan Brown via Discuss < > discuss@lists.openscad.org> wrote: > > On 8/7/2025 10:47 PM, Revar Desmera via Discuss wrote: > > Workaround: `len([for(x=obj)x])` > > > Yeah, I thought about that, but if there's a need for a "workaround" then > we should just make it work. > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Fri, Aug 8, 2025 12:15 PM

On 8/8/2025 2:25 AM, Steve Lelievre via Discuss wrote:

To me an object’s key count is conceptually different to length

I don't entirely disagree, but what else would "length" mean?

a new built-in function that returns a vector of the keys. Then we
could do things like

echo(len(keys(myObj));

but also process them sequentially

for(i = keys(myObj)) … something …myObj[i];

Iteration already works directly; if you say "for (k = myObj)" k is set
to each key in sequence.

Your "keys()" is thus

function keys(obj) = [ for (k = obj) k ];

... but that's not really all that interesting.

That's why len( [ for (k = obj) k ] ) works.

On 8/8/2025 2:25 AM, Steve Lelievre via Discuss wrote: > To me an object’s key count is conceptually different to length I don't entirely disagree, but what *else* would "length" mean? > a new built-in function that returns a vector of the keys. Then we > could do things like > > echo(len(keys(myObj)); > > but also process them sequentially > > for(i = keys(myObj)) … something …myObj[i]; Iteration already works directly; if you say "for (k = myObj)" k is set to each key in sequence. Your "keys()" is thus function keys(obj) = [ for (k = obj) k ]; ... but that's not really all that interesting. That's why len( [ for (k = obj) k ] ) works.
SL
Steve Lelievre
Fri, Aug 8, 2025 12:22 PM

Ah okay,

Thanks for teaching me.

Steve

On Fri, 8 Aug 2025 at 08:15, Jordan Brown openscad@jordan.maileater.net
wrote:

On 8/8/2025 2:25 AM, Steve Lelievre via Discuss wrote:

To me an object’s key count is conceptually different to length

I don't entirely disagree, but what else would "length" mean?

a new built-in function that returns a vector of the keys. Then we could
do things like

echo(len(keys(myObj));

but also process them sequentially

for(i = keys(myObj)) … something …myObj[i];

Iteration already works directly; if you say "for (k = myObj)" k is set to
each key in sequence.

Your "keys()" is thus

function keys(obj) = [ for (k = obj) k ];

... but that's not really all that interesting.

That's why len( [ for (k = obj) k ] ) works.

Ah okay, Thanks for teaching me. Steve On Fri, 8 Aug 2025 at 08:15, Jordan Brown <openscad@jordan.maileater.net> wrote: > > On 8/8/2025 2:25 AM, Steve Lelievre via Discuss wrote: > > To me an object’s key count is conceptually different to length > > > I don't entirely disagree, but what *else* would "length" mean? > > > a new built-in function that returns a vector of the keys. Then we could > do things like > > echo(len(keys(myObj)); > > but also process them sequentially > > for(i = keys(myObj)) … something …myObj[i]; > > > Iteration already works directly; if you say "for (k = myObj)" k is set to > each key in sequence. > > Your "keys()" is thus > > function keys(obj) = [ for (k = obj) k ]; > > ... but that's not really all that interesting. > > That's why len( [ for (k = obj) k ] ) works. > > >
V
vulcan_@mac.com
Fri, Aug 8, 2025 2:36 PM

Steve Lelievre wrote:

To me an object’s key count is conceptually different to length

Len() currently returns the number of characters in a string, and the number of top-level elements of a vector:

v = [ 1,2, [3,4] ];
echo( len( v ) ); // ECHO: 3

built-in function that returns a vector of the keys.

too easy to do

keys = [ for( o=object( this=”that” ) o ) ]

will make a vector of strings, where the strings are the names of the object’s elements in order of creation

echo(len(keys(myObj));

I would like to see a built in member function:

o=object( this=”that” );
k = o.keys();
echo( k ); // ECHO: [“that“]

but then we would also want

ell = o.len()

wouldn’t we?

Steve Lelievre wrote: > To me an object’s key count is conceptually different to length Len() currently returns the number of characters in a string, and the number of top-level elements of a vector: `v = [ 1,2, [3,4] ];`\ `echo( len( v ) ); // ECHO: 3` > > built-in function that returns a vector of the keys. too easy to do `keys = [ for( o=object( this=”that” ) o ) ]` will make a vector of strings, where the strings are the names of the object’s elements in order of creation > echo(len(keys(myObj)); I would like to see a built in member function: `o=object( this=”that” );`\ `k = o.keys();`\ `echo( k ); // ECHO: [“that“]` but then we would also want `ell = o.len()` wouldn’t we?
JB
Jordan Brown
Fri, Aug 8, 2025 3:01 PM

I would like to see a built in member function:

|o=object( this=”that” );
k = o.keys();
echo( k ); // ECHO: [“that“]|

but then we would also want

|ell = o.len()|

wouldn’t we?

I would be hesitant to put predefined member functions on objects,
because that intrudes on namespace that the user owns.

(And of course we don't have a $this-like mechanism yet, so this is
premature.)

> I would like to see a built in member function: > > |o=object( this=”that” ); > k = o.keys(); > echo( k ); // ECHO: [“that“]| > > but then we would also want > > |ell = o.len()| > > wouldn’t we? > I would be hesitant to put predefined member functions on objects, because that intrudes on namespace that the user owns. (And of course we don't have a $this-like mechanism yet, so this is premature.)