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.
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.
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/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.
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
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
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.
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?
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.)