discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

possible bug when using echo inside of a function.

JD
John David
Sun, Feb 9, 2025 4:31 AM

Hello,

I've been plagued for a while trying to figure out why every echo statement
I add to a function gives me "WARNING: Assignment without variable name
undef in file <158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad>
...", but when I set a dummy variable to the echo statement, it then
works.  I now kinda get what is happening, but is this a bug or a feature?
Along those lines, what is considered bast practice?

EBo --

Hello, I've been plagued for a while trying to figure out why every echo statement I add to a function gives me "WARNING: Assignment without variable name undef in file <158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad> ...", but when I set a dummy variable to the echo statement, it then works. I now kinda get what is happening, but is this a bug or a feature? Along those lines, what is considered bast practice? EBo --
JB
Jordan Brown
Sun, Feb 9, 2025 6:04 AM

On Feb 8, 2025, at 8:32 PM, John David via Discuss <discuss@lists.openscad.org> wrote:

I've been plagued for a while trying to figure out why every echo statement I add to a function gives me "WARNING: Assignment without variable name undef in file...", but when I set a dummy variable to the echo statement, it then works. I now kinda get what is happening, but is this a bug or a feature? Along those lines, what is considered bast practice?

It would be good if you could give an example of a function that causes the problem.

Echo clauses in expressions are an … unusual … syntax. Anywhere that an expression is allowed, you can prefix the expression with any or all of echo(), assert(), or let(). echo() and assert() have no effect on the expression that follows; let() sets a variable that is visible while evaluating that expression.

Typical use might be

function foo(a) = echo(“foo(“, a, “)”) a + 5;

function sign(v) = v < 0

? echo(“sign(neg)”) -1

: v > 0

? echo(“sign(pos)”) 1

: echo(“sign(zero)”) 0;

function bar(x) = let(xplus1 = x+1) xplus1 * 2;

function notneg(v) = assert(v >= 0) v;

Occasionally it may be desirable to apply an assertion at assignment time, rather than waiting until module-invocation time (when normal assert() is called). Also, there may be contexts where you don’t actually need a value, and for assert() and echo() you can omit it.

module blah(n) {

junk = assert(is_num(n), “n must be a number”);

x = n + 1;

// …

}

Hope that helps.

GH
gene heskett
Sun, Feb 9, 2025 7:09 AM

On 2/8/25 23:32, John David via Discuss wrote:

Hello,

I've been plagued for a while trying to figure out why every echo statement
I add to a function gives me "WARNING: Assignment without variable name
undef in file <158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad>
...", but when I set a dummy variable to the echo statement, it then
works.  I now kinda get what is happening, but is this a bug or a feature?
Along those lines, what is considered bast practice?

EBo --

Probably not enough commas in the list you want to echo. I start with a
line number, then the text name = in quotes, comma and the name of the
var, wash, rinse, repeat, till out of vars to look at.

So it winds up looking like this:

echo(491," ew=",ew," ey=",ey," ez=,ez);  etc,etc);


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
On 2/8/25 23:32, John David via Discuss wrote: > Hello, > > I've been plagued for a while trying to figure out why every echo statement > I add to a function gives me "WARNING: Assignment without variable name > undef in file <158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad> > ...", but when I set a dummy variable to the echo statement, it then > works. I now kinda get what is happening, but is this a bug or a feature? > Along those lines, what is considered bast practice? > > EBo -- Probably not enough commas in the list you want to echo. I start with a line number, then the text name = in quotes, comma and the name of the var, wash, rinse, repeat, till out of vars to look at. So it winds up looking like this: echo(491," ew=",ew," ey=",ey," ez=,ez);  etc,etc); > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org Cheers, Gene Heskett, CET. -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author, 1940) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis
NH
nop head
Sun, Feb 9, 2025 10:27 AM

I have a function that I use to echo any expression or part of an
expression without changing it.

function echoit(x) = echo(x) x;      //! Echo expression and return it,
useful for debugging

Then I don't need any dummy variables.

On Sun, 9 Feb 2025 at 07:09, gene heskett via Discuss <
discuss@lists.openscad.org> wrote:

On 2/8/25 23:32, John David via Discuss wrote:

Hello,

I've been plagued for a while trying to figure out why every echo

statement

I add to a function gives me "WARNING: Assignment without variable name
undef in file

<158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad>

...", but when I set a dummy variable to the echo statement, it then
works.  I now kinda get what is happening, but is this a bug or a

feature?

Along those lines, what is considered bast practice?

EBo --

Probably not enough commas in the list you want to echo. I start with a
line number, then the text name = in quotes, comma and the name of the
var, wash, rinse, repeat, till out of vars to look at.

So it winds up looking like this:

echo(491," ew=",ew," ey=",ey," ez=,ez);  etc,etc);


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis

OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I have a function that I use to echo any expression or part of an expression without changing it. function echoit(x) = echo(x) x; //! Echo expression and return it, useful for debugging Then I don't need any dummy variables. On Sun, 9 Feb 2025 at 07:09, gene heskett via Discuss < discuss@lists.openscad.org> wrote: > > On 2/8/25 23:32, John David via Discuss wrote: > > Hello, > > > > I've been plagued for a while trying to figure out why every echo > statement > > I add to a function gives me "WARNING: Assignment without variable name > > undef in file > <158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad> > > ...", but when I set a dummy variable to the echo statement, it then > > works. I now kinda get what is happening, but is this a bug or a > feature? > > Along those lines, what is considered bast practice? > > > > EBo -- > > Probably not enough commas in the list you want to echo. I start with a > line number, then the text name = in quotes, comma and the name of the > var, wash, rinse, repeat, till out of vars to look at. > > So it winds up looking like this: > > echo(491," ew=",ew," ey=",ey," ez=,ez); etc,etc); > > > > > > > > _______________________________________________ > > OpenSCAD mailing list > > To unsubscribe send an email to discuss-leave@lists.openscad.org > > Cheers, Gene Heskett, CET. > -- > "There are four boxes to be used in defense of liberty: > soap, ballot, jury, and ammo. Please use in that order." > -Ed Howdershelt (Author, 1940) > If we desire respect for the law, we must first make the law respectable. > - Louis D. Brandeis > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JD
John David
Sun, Feb 9, 2025 6:08 PM

I'll do up some examples in a bit, but I should ask what version are you
running?  I'm currently using 2021.01.

I have to leave for a bit.  I'll post this later.

EBo --

On Sun, Feb 9, 2025 at 5:28 AM nop head via Discuss <
discuss@lists.openscad.org> wrote:

I have a function that I use to echo any expression or part of an
expression without changing it.

function echoit(x) = echo(x) x;      //! Echo expression and return it,
useful for debugging

Then I don't need any dummy variables.

On Sun, 9 Feb 2025 at 07:09, gene heskett via Discuss <
discuss@lists.openscad.org> wrote:

On 2/8/25 23:32, John David via Discuss wrote:

Hello,

I've been plagued for a while trying to figure out why every echo

statement

I add to a function gives me "WARNING: Assignment without variable name
undef in file

<158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad>

...", but when I set a dummy variable to the echo statement, it then
works.  I now kinda get what is happening, but is this a bug or a

feature?

Along those lines, what is considered bast practice?

EBo --

Probably not enough commas in the list you want to echo. I start with a
line number, then the text name = in quotes, comma and the name of the
var, wash, rinse, repeat, till out of vars to look at.

So it winds up looking like this:

echo(491," ew=",ew," ey=",ey," ez=,ez);  etc,etc);


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis

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

I'll do up some examples in a bit, but I should ask what version are you running? I'm currently using 2021.01. I have to leave for a bit. I'll post this later. EBo -- On Sun, Feb 9, 2025 at 5:28 AM nop head via Discuss < discuss@lists.openscad.org> wrote: > I have a function that I use to echo any expression or part of an > expression without changing it. > > function echoit(x) = echo(x) x; //! Echo expression and return it, > useful for debugging > > Then I don't need any dummy variables. > > On Sun, 9 Feb 2025 at 07:09, gene heskett via Discuss < > discuss@lists.openscad.org> wrote: > >> >> On 2/8/25 23:32, John David via Discuss wrote: >> > Hello, >> > >> > I've been plagued for a while trying to figure out why every echo >> statement >> > I add to a function gives me "WARNING: Assignment without variable name >> > undef in file >> <158,/home/ebo/Downloads/greenhouse_CAD/Climate_Battery.scad> >> > ...", but when I set a dummy variable to the echo statement, it then >> > works. I now kinda get what is happening, but is this a bug or a >> feature? >> > Along those lines, what is considered bast practice? >> > >> > EBo -- >> >> Probably not enough commas in the list you want to echo. I start with a >> line number, then the text name = in quotes, comma and the name of the >> var, wash, rinse, repeat, till out of vars to look at. >> >> So it winds up looking like this: >> >> echo(491," ew=",ew," ey=",ey," ez=,ez); etc,etc); >> >> >> > >> > >> > _______________________________________________ >> > OpenSCAD mailing list >> > To unsubscribe send an email to discuss-leave@lists.openscad.org >> >> Cheers, Gene Heskett, CET. >> -- >> "There are four boxes to be used in defense of liberty: >> soap, ballot, jury, and ammo. Please use in that order." >> -Ed Howdershelt (Author, 1940) >> If we desire respect for the law, we must first make the law respectable. >> - Louis D. Brandeis >> _______________________________________________ >> 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 >
TP
Torsten Paul
Sun, Feb 9, 2025 6:25 PM

On 09.02.25 08:09, gene heskett via Discuss wrote:

echo(491," ew=",ew," ey=",ey," ez=,ez);  etc,etc);

Note that you can name the parameters directly which gives an
even more concise output:

ew = 10;
ey = 20;
ez = 30;

echo(491," ew=",ew," ey=",ey," ez=",ez);
// ECHO: 491, " ew=", 10, " ey=", 20, " ez=", 30

echo(491, ew = ew, ey = ey, ez = ez);
// ECHO: 491, ew = 10, ey = 20, ez = 30

ciao,
Torsten.

On 09.02.25 08:09, gene heskett via Discuss wrote: > > echo(491," ew=",ew," ey=",ey," ez=,ez);  etc,etc); Note that you can name the parameters directly which gives an even more concise output: ew = 10; ey = 20; ez = 30; echo(491," ew=",ew," ey=",ey," ez=",ez); // ECHO: 491, " ew=", 10, " ey=", 20, " ez=", 30 echo(491, ew = ew, ey = ey, ez = ez); // ECHO: 491, ew = 10, ey = 20, ez = 30 ciao, Torsten.
JB
Jordan Brown
Sun, Feb 9, 2025 6:34 PM

I'll do up some examples in a bit, but I should ask what version are you running?  I'm currently using 2021.01.

I usually do stuff in a recent nightly, but I don’t think this stuff has changed since 2021.01.

> I'll do up some examples in a bit, but I should ask what version are you running? I'm currently using 2021.01. I usually do stuff in a recent nightly, but I don’t think this stuff has changed since 2021.01.
JD
John David
Sun, Feb 9, 2025 9:29 PM

Here is a simple function I wrote to test:

b =test_echo1("********* bla");
c =test_echo2("********* woof");

function test_echo1(msg) =
let(
message = msg,
echo(message),
a=1
) a;

function test_echo2(msg) =
let(
message = msg,
z = echo(message),
a=1
) a;

test_echo1 will write out "********* bla", and then fail.  if you comment
out the first line, test_echo2 writes out "********* woof" and continues.
So, why do I need to set a return for the echo?  Is there a way to set it
to ignore ('_ = echo..." maybe)?

EBo --

On Sun, Feb 9, 2025 at 1:34 PM Jordan Brown openscad@jordan.maileater.net
wrote:

I'll do up some examples in a bit, but I should ask what version are you

running?  I'm currently using 2021.01.

I usually do stuff in a recent nightly, but I don’t think this stuff has
changed since 2021.01.

Here is a simple function I wrote to test: b =test_echo1("********* bla"); c =test_echo2("********* woof"); function test_echo1(msg) = let( message = msg, echo(message), a=1 ) a; function test_echo2(msg) = let( message = msg, z = echo(message), a=1 ) a; test_echo1 will write out "********* bla", and then fail. if you comment out the first line, test_echo2 writes out "********* woof" and continues. So, why do I need to set a return for the echo? Is there a way to set it to ignore ('_ = echo..." maybe)? EBo -- On Sun, Feb 9, 2025 at 1:34 PM Jordan Brown <openscad@jordan.maileater.net> wrote: > > > I'll do up some examples in a bit, but I should ask what version are you > running? I'm currently using 2021.01. > > I usually do stuff in a recent nightly, but I don’t think this stuff has > changed since 2021.01. >
TP
Torsten Paul
Sun, Feb 9, 2025 11:37 PM

On 09.02.25 22:29, John David via Discuss wrote:

So, why do I need to set a return for the echo?

That has nothing to do with echo() but with the
let() it's in. This is meant to assign variable.
Your usage does not give it a variable name,
hence the warning.

As nophead showed earlier, the echo() function can
be chained similar to the list comprehensions and
will return the value behind, printing the message
as side effect,

function f(x) = let(y = 2 * x) echo(x=x, y=y) y;
echo(f_result = f(3));

I guess the usage for the case above would be:

function test_echo1(msg) =
let(
message = msg,
a=1
)
echo(message)
a;

ciao,
Torsten.

On 09.02.25 22:29, John David via Discuss wrote: > So, why do I need to set a return for the echo? That has nothing to do with echo() but with the let() it's in. This is meant to assign variable. Your usage does not give it a variable name, hence the warning. As nophead showed earlier, the echo() function can be chained similar to the list comprehensions and will return the value behind, printing the message as side effect, function f(x) = let(y = 2 * x) echo(x=x, y=y) y; echo(f_result = f(3)); I guess the usage for the case above would be: function test_echo1(msg) = let( message = msg, a=1 ) echo(message) a; ciao, Torsten.
AM
Adrian Mariano
Mon, Feb 10, 2025 12:36 AM

I would argue that it has everything to do with echo() and that calling
echo a "function" is misleading.  First of all there are two echos.  One
echo is a module.  If you write

r=4;
sphere(r=r);
echo(r=r);

then you're using echo as a module.  The other form of echo, however, is
neither a module nor a function but some other kind of object.  It is
module-like in that it takes "child" which must be a value (e.g. a list,
scalar, etc).  If the child is absent the child value defaults to zero.
Then echo has a side effect (displaying something to the console) and
finally in a function-like fashion it returns its child.  So there always
has to be a left hand side to "use" the return.

A similar distinction exists with assertions.  If you write this:

assert(is_valid(x));
y=f(x);

you may be puzzled to find that you get errors from f that your input x is
bad even though you protected it with an assertion.  I know I was extremely
puzzled by this.  The problem is that modules run after assignments, so the
y=f(x) assignment runs first, before the assert, which is a module in this
case.  You can fix this by doing

dummy = assert(is_valid(x));
y=f(x);

which uses a form of assert() that is also a not-a-function object like
echo, which takes a child value that is allowed to be omitted, but returns
its child.  Written this way, the assert runs before the y=f(x) assignment
and properly catches an invalid value.

And in fact this same problem can arise with echos.  Suppose my code has a
bug in f(x) and I try to gain insight with

echo(x=x);
y=f(x);

The echo runs after the y=f(x) assignment and if assertions inside the
y=f(x) assignment stop execution, the echo never runs.  You can fix this
the same way, with dummy=echo(x=x).

There's no special role or need for let in all of this.  Putting a left
hand side on the expressions changes the interpretation from echo as a
module to echo as a not-a-function object.  Might be nice if we had an
official name for these kinds of objects to better explain their behavior.
If you want to use this kind of object outside of let you can do so just by
using it in a context where you use a return, which signals that it's not a
module.  I suppose in let the one difference is that modules aren't
allowed, so you get an warning/error instead of a shift in behavior to the
module form when you don't use the return.

On Sun, Feb 9, 2025 at 6:37 PM Torsten Paul via Discuss <
discuss@lists.openscad.org> wrote:

On 09.02.25 22:29, John David via Discuss wrote:

So, why do I need to set a return for the echo?

That has nothing to do with echo() but with the
let() it's in. This is meant to assign variable.
Your usage does not give it a variable name,
hence the warning.

As nophead showed earlier, the echo() function can
be chained similar to the list comprehensions and
will return the value behind, printing the message
as side effect,

function f(x) = let(y = 2 * x) echo(x=x, y=y) y;
echo(f_result = f(3));

I guess the usage for the case above would be:

function test_echo1(msg) =
let(
message = msg,
a=1
)
echo(message)
a;

ciao,
Torsten.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I would argue that it has everything to do with echo() and that calling echo a "function" is misleading. First of all there are two echos. One echo is a module. If you write r=4; sphere(r=r); echo(r=r); then you're using echo as a module. The other form of echo, however, is neither a module nor a function but some other kind of object. It is module-like in that it takes "child" which must be a value (e.g. a list, scalar, etc). If the child is absent the child value defaults to zero. Then echo has a side effect (displaying something to the console) and finally in a function-like fashion it returns its child. So there always has to be a left hand side to "use" the return. A similar distinction exists with assertions. If you write this: assert(is_valid(x)); y=f(x); you may be puzzled to find that you get errors from f that your input x is bad even though you protected it with an assertion. I know I was extremely puzzled by this. The problem is that modules run after assignments, so the y=f(x) assignment runs first, before the assert, which is a module in this case. You can fix this by doing dummy = assert(is_valid(x)); y=f(x); which uses a form of assert() that is also a not-a-function object like echo, which takes a child value that is allowed to be omitted, but returns its child. Written this way, the assert runs before the y=f(x) assignment and properly catches an invalid value. And in fact this same problem can arise with echos. Suppose my code has a bug in f(x) and I try to gain insight with echo(x=x); y=f(x); The echo runs after the y=f(x) assignment and if assertions inside the y=f(x) assignment stop execution, the echo never runs. You can fix this the same way, with dummy=echo(x=x). There's no special role or need for let in all of this. Putting a left hand side on the expressions changes the interpretation from echo as a module to echo as a not-a-function object. Might be nice if we had an official name for these kinds of objects to better explain their behavior. If you want to use this kind of object outside of let you can do so just by using it in a context where you use a return, which signals that it's not a module. I suppose in let the one difference is that modules aren't allowed, so you get an warning/error instead of a shift in behavior to the module form when you don't use the return. On Sun, Feb 9, 2025 at 6:37 PM Torsten Paul via Discuss < discuss@lists.openscad.org> wrote: > On 09.02.25 22:29, John David via Discuss wrote: > > So, why do I need to set a return for the echo? > > That has nothing to do with echo() but with the > let() it's in. This is meant to assign variable. > Your usage does not give it a variable name, > hence the warning. > > As nophead showed earlier, the echo() function can > be chained similar to the list comprehensions and > will return the value behind, printing the message > as side effect, > > function f(x) = let(y = 2 * x) echo(x=x, y=y) y; > echo(f_result = f(3)); > > I guess the usage for the case above would be: > > function test_echo1(msg) = > let( > message = msg, > a=1 > ) > echo(message) > a; > > ciao, > Torsten. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >