[OpenSCAD] eval( ) ???

doug moen doug at moens.org
Mon May 18 21:37:25 EDT 2015


Runsun said:

> Also would be great if, other than *data.a*, we can do *x=data["a"]*,
> *x=data("a")* (in case it needs to contain space) or even *x=data(a)*.


I would vote for data["a"] or data.("a")


> One more thing, variable name like 3a, 4b are allowed in openscad. Will
> data.3a also be allowed ?


Of course.


> And, on top of that, we also need:
>    keys(data) ==> ["a","b"]
>    values(data) ==> [10,20]
> or data.keys, data.values, such that they can be iterated over.


I'll grant the need for keys(data). But values is just
   [for (k=keys(data)) data[k]]
and I don't know that it would be needed often enough to justify as a
builtin.

This brings up: if we do *[ for(x=data) ...]*, what would x stands for.


OpenSCAD scripts are the model for what objects are. An object is a set of
named fields, and a sequence of shapes. Consider:

lollypop = {

  radius = 3;

  diameter = 1;

  height = 10;


  translate([0,0,height]) sphere(r=radius);

  cylinder(d=diameter,h=height);

};


You can use 'lollypop' in any context expecting a shape.

Or you can customize lollypop, eg with a longer stick:

   lollypop(height=20)


You can accomplish something similar with a module definition, but objects
are easier to understand and use.


So lollypop contains two shapes, and behaves like a list of those shapes.

   len(lollypop) == 2

   lollypop[0] == translate([0,0,10]) sphere(r=3)

   lollypop[1] == cylinder(d=1,h=10)

and this answers what "for (x=lollypop) ..." means.


Doug.

On 18 May 2015 at 17:14, runsun <runsun at gmail.com> wrote:

> doug.moen wrote
> > Now I'll copy and paste part of Yona's post, with terminology and syntax
> > changed to correspond to my current OpenSCAD 2 design:
> >
> > // If we consider { ... } to be an "inline object", you get associative
> >> arrays for free:
> >> data = {
> >>    a = 10;
> >>    b = 20;
> >> }
> >> echo(data.a);
> >> // You can create a copy with new values as well
> >> data2 = data(b=30, c=100);
>
> I like this idea. For the on-the-fly local var assignment in data(b=30,
> c=100), openscad seems to already have this capacity:
>
>   echo( b=30, c=100 );
>
> b,c are local variables created at the run time.
>
> Also would be great if, other than *data.a*, we can do *x=data["a"]*,
> *x=data("a")* (in case it needs to contain space) or even *x=data(a)*.
>
> One more thing, variable name like 3a, 4b are allowed in openscad. Will
> data.3a also be allowed ?
>
> And, on top of that, we also need:
>
>    keys(data) ==> ["a","b"]
>    values(data) ==> [10,20]
>
> or data.keys, data.values, such that they can be iterated over.
>
> This brings up: if we do *[ for(x=data) ...]*, what would x stands for.
>
>
>
> -----
>
> $  Runsun Pan, PhD
>
> $ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  )
>
> $ -- hash parameter model: here , here
>
> $ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly
>
>
>
>
> --
> View this message in context:
> http://forum.openscad.org/eval-tp12627p12700.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150518/1bf1e959/attachment-0002.html>


More information about the Discuss mailing list