Loading

Is there a way for the code to read the name of a variable?

Please Login to Comment

I'm aware there are workarounds to this problem, but I was wondering if it is possible to read/extract the name of a variable, not just its value?

eg
box_height =24;

function aboutVariable(X)=
echo(str("This variablei's name is ", extractName(X), ". value = ", X));

aboutVariable(box_height); // = "This variablei's name is box_height. value = 24"

You're barking up the wrong tree because in the function, the variable's name is 'X'.

Imagine if you had multiple levels of functions, which level would the name be supposed to be referring to? What happens when you say Y=X. Is Y now called X or is it called Y?

I don't think there's any language which supports this kind of thing though your could (maybe) bodge something together in an object oriented language but it's not really very meaningful. You might be better off just passing in an extra parameter to your function.

I personally have not seen a way to show the variable as a literal. Since the variable takes on the variable X in the function, you already lost what you want.

When I control the source... my workaround is to use arrays to make variable sets... and just add a description.
//screw heads
screw_hole_dia = 0; shead_bottom=1; shead_top = 2; screw_head_hieght = 3; screw_desc = 4;

//table of variables
aryboxtable = [
["fh1" ,[ 5.0, 5.4, 9, 3.6, "flat"]],
["fh2" ,[ 5.0, 6, 9.2, 3, "flat"]],
["hx1" ,[ 5.0, 6, 10.4, 3, "hex 8-32"]],
["hx2" ,[ 5.0, 6, 11.0, 3, "hex 10-24"]],
["hx3" ,[ 5.1, 6, 9.4, 3, "hex M5"]],
["rh1" ,[ 5.0, 8.5, 8.5, 4 - 1.6, "rnd"]],
["rh2" ,[ 4.6, 9.8, 9.8, 4 - 1.6, "rnd"]]
];

I have this function
function fcnpickscrew(usetable, boxtype) = usetable[search([boxtype],usetable)[0]][1];

In customizer you can select the variable to use... or just set it.
aryscrewtype1 = fcnpickscrew(aryboxtable, "fh2");

Then just reference the attribute
aryscrewtype1[shead_top]
aryscrewtype1[screw_desc]

To tell the truth, I wasn't particularly optimistic that it would be possible. But I would have been pleasantly surprised to be wrong.

I had thought of a workaround such as you describe, however in this case, I don't know what the variables are called in advance, so it just means a little more work for whoever has to input the arguments at the time of use, no biggy!

Yes, you can echo the name and value of a variable.

x = 5;
echo (x = x);
y = x*2.25;
echo (y = y);

That's both interesting and rather annoying.

I've been typing: echo("x = ",x); since forever! Never would have occurred to me to input x=x. I'll be sure to use that in the future.

On the downside, It shows openscad can do the very thing I want, but exclusively inside an echo(). So It is of no use :(

No use? How so?

Just in case it's because you are using arrays...

//table of variables
aryboxtable = [
["fh1" ,[ 5.0, 5.4, 9, 3.6, "flat"]],
["fh2" ,[ 5.0, 6, 9.2, 3, "flat"]],
["hx1" ,[ 5.0, 6, 10.4, 3, "hex 8-32"]],
["hx2" ,[ 5.0, 6, 11.0, 3, "hex 10-24"]],
["hx3" ,[ 5.1, 6, 9.4, 3, "hex M5"]],
["rh1" ,[ 5.0, 8.5, 8.5, 4 - 1.6, "rnd"]],
["rh2" ,[ 4.6, 9.8, 9.8, 4 - 1.6, "rnd"]]
];
echo (aryboxtable=aryboxtable);

Which produces:

ECHO: aryboxtable = [["fh1", [5, 5.4, 9, 3.6, "flat"]], ["fh2", [5, 6, 9.2, 3, "flat"]], ["hx1", [5, 6, 10.4, 3, "hex 8-32"]], ["hx2", [5, 6, 11, 3, "hex 10-24"]], ["hx3", [5.1, 6, 9.4, 3, "hex M5"]], ["rh1", [5, 8.5, 8.5, 2.4, "rnd"]], ["rh2", [4.6, 9.8, 9.8, 2.4, "rnd"]]]

Interesting ! Thanks for the clue.