Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function body value names not looked up in function parameter scope #4

Closed
kwikius opened this issue Oct 1, 2022 · 2 comments
Closed

Comments

@kwikius
Copy link
Owner

kwikius commented Oct 1, 2022

f0 = function (s) module cube(s);
m0 = f0(10);
m0();

f1 = function () module cube([5,10,20]);
m1 = f1();
m1();

WARNING: Ignoring unknown variable 's' in file test.scad, line 1

However there is a graphical output of the cube from m1();

@kwikius kwikius changed the title Functtion body value names not looked up in function parameter scope Function body value names not looked up in function parameter scope Oct 2, 2022
kwikius added a commit that referenced this issue Oct 19, 2022
Prolem with function is that function context is invalid after the return
so evaluation must be  done in the literal or the function args context must be preserved.
This fixes  the initial example , but won't fix anonymous modules
@kwikius
Copy link
Owner Author

kwikius commented Oct 19, 2022

Fixed by ad19578

For anonymous modules you can use the following syntax. Essentially you need to create the module_literal and get a reference to it in local scope, then return a reference to the module with function call args

function f (pos ,size) = 
   let ( m1 = module (p, s) { translate(p){cube(s);}})
module m1(pos,size);

m = f([100,50,50],[20,30,40]);

m();

@kwikius kwikius closed this as completed Oct 19, 2022
@kwikius
Copy link
Owner Author

kwikius commented Dec 2, 2022

For functions with arguments returning a module with arguments,
e.g https://libera.irclog.whitequark.org/openscad/2022-10-01#33072095 (There proposed using pseudo syntax as
fm = function (x) module (y) cube([x, y, 2]); fm(3)(5); )

Currently this can be done as follows:
Any function arguments used in the returned module should be added as default arguments to the inner module m parameters as follows. Here the argument x of function fm, used in the returned module m is made a default argument of the module literal parameter x1 and the parameter x is then used in the module arguments

fm = function(x) 
  let ( m = module(y,x1 = x) cube([x1,y,20]) )
m;
(fm(30))(50);  // instantiate the module returned from fm 

This can be shortened to

fm = function(x) 
  module(y,x1 = x) cube([x1,y,20]);

It would be possible to optimise this but this workaround seems satisfactory at the moment.
Also currently default arguments need to follow non default arguments.

@kwikius kwikius reopened this Dec 2, 2022
@kwikius kwikius closed this as completed Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant