you could probably extend the module_alias syntax to parameters
I think that is how module references should work, i.e. the same as
function references. If you give all the parameters at the point of
assigning the reference it becomes a reference to an instantiated module,
which is not the same as a module.
On Fri, 2 Sept 2022 at 07:34, Andy Little via Discuss <
discuss@lists.openscad.org> wrote:
---------- Forwarded message ----------
From: kwikius@yahoo.com
To: discuss@lists.openscad.org
Cc:
Bcc:
Date: Fri, 2 Sep 2022 06:33:40 +0000
Subject: [OpenSCAD] Re: Making modules first class without breaking
existing Openscad code
Let's look at a more complex example. Here's a module that creates a
wide-screen television based on its diagonal and its thickness.
module television(diagonal, thickness) {
// Ratio for a wide-screen TV
nominal_h = 9;
nominal_w = 16;
nominal_diagonal = sqrt(nominal_h^2 + nominal_w^2);
// Multiplier for this TV
ratio = diagonal/nominal_diagonal;
// Resulting height and width
h = nominal_h * ratio;
w = nominal_w * ratio;
cube([w, thickness, h]);
// add some feet
translate([0,0,-1]) cube([1,thickness,1]);
translate([w-1,0,-1]) cube([1,thickness,1]);
echo(h=h, w=w, diagonal=diagonal);
}
x = module television(55,2);
What are the "members" of x?
diagonal = x.diagonal; // (55)
thickness = x.thickness; // (2)
nominal_h = x.nominal_h; //(9)
nominal_w = x.nominal_w; // (16)
nominal_diagonal = x.nominal_diagonal (18.3576)
ratio = x.ratio; //(2.99604)
h = x.h; //(26.9644)
w = x.w; //(47.9367)
When would the echo() happen?
x();
(since echo(...) is actually a single_module_instantiation. see
https://github.com/openscad/openscad/blob/master/src/core/parser.y#L314
If I'm understanding correctly, you're suggesting that x.diagonal=55 and
x.thickness=2, and the echo() has not yet happened, and will happen when
and if x() happens.
Yes !
And I think x() always creates a 55-unit-diagonal television, 2 units
thick.
Is that what you intend?
Yes !
Over the last few years, a number of people have been working on ideas
related to this.
You can find some of the thinking at
https://github.com/openscad/openscad/pull/3077 (function
literals/references)
Very nice and also working in current openscad. The module_alias feature
is intended to do something similar for modules. Perhaps module_alias
should be renamed to module_literal ?
https://github.com/openscad/openscad/pull/3087 ("object literals")
The module alias is complimentary. You can use a module_alias in the body
of an object_literal I think or seee no reason why not.
https://github.com/openscad/openscad/issues/3088 (various related topics)
you could presumably use a module_alias in a script also
https://github.com/openscad/openscad/pull/3956 (rendering geometry into
data)
fun and potentially very useful but doesn’t really interact with the
module_alias since it transfoms a module_instantiation into pure data. A
quick read of the implementation suggest it instantiates module(s) and then
removes them from the AST. That might be a little hairy!
Anyway I don't think it has an effect on the module_alias feature
I tried to pull some of those concepts together into a coherent whole at
https://docs.google.com/document/d/14siqbr9CyGuA2tY6dvTqiF3Hf9zFMyefk7YdaUJSn3Q
That doc focuses on geometry-as-data, but has a brief discussion of
module references (sometimes maybe called module literals) toward the
bottom - mostly to point out that they are an orthogonal concept.
I've only just started to push that model, and so I can't claim that I
have buy-in, but based on some conversations today I think maybe there's
interest.
I don't think anything in there is precisely aligned with what you're
thinking of, but I think it's close and might be close enough.
from the doc
m = module(parameters) {...};
m is a reference to that module, can be invoked like so:
m(arguments);
looks like an alternative syntax for defining modules? but how do you
refer to already defined modules?
you could probably extend the module_alias syntax to parameters
m = module(height) cube([1,1,height]);
m(20); // --> cube([1,1,20]);
---------- Forwarded message ----------
From: Andy Little via Discuss discuss@lists.openscad.org
To: discuss@lists.openscad.org
Cc: kwikius@yahoo.com
Bcc:
Date: Fri, 2 Sep 2022 06:33:40 +0000
Subject: [OpenSCAD] Re: Making modules first class without breaking
existing Openscad code
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org
syntactically you can do all the following without instantiating the module
m = module name(args); // a module_alias without parameters
m2 = module(params) name(args); // a module_alias with parameters
m3 = module(params) { inner_input }; // an anonymous module referred to by the module_alias m3
(without bison complaining)
to instantiate
m();
m2(a,b,c);
m3(a,b,c);
Substitute module_alias with module_literal or module_referenceperhaps
Its not a reference in the C++ sense, so maybe shouldnt be called a reference.
Its specifically a variable that refers to an uninstantiated module.
(You can also call it, to instantiate the module, somewhat like a function object in C++)