discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Adding an echo Statement inside a function

NS
Nathan Sokalski
Sat, Oct 28, 2023 4:07 PM

Most people (including me) use echo statements primarily for debugging purposes. This is usually pretty easy, except for inside a function. Because a function is basically an assignment (it uses the = operator), I'm not sure what the best way to debug a complex function is (for example, determining the value that gets assigned to a variable in the let()). Any ideas?

Nathan Sokalski
njsokalski@hotmail.commailto:njsokalski@hotmail.com

Most people (including me) use echo statements primarily for debugging purposes. This is usually pretty easy, except for inside a function. Because a function is basically an assignment (it uses the = operator), I'm not sure what the best way to debug a complex function is (for example, determining the value that gets assigned to a variable in the let()). Any ideas? Nathan Sokalski njsokalski@hotmail.com<mailto:njsokalski@hotmail.com>
JG
Jonathan Gilbert
Sat, Oct 28, 2023 4:33 PM

I've done it a few ways:

function x() =
let(
assign_var1 = 1,
assign_var2 = 2
)
echo( assign_var1, assign_var2 )
[ assign_var1, assign_var2 ];

...treats the echo() as an inline part of the function, just like let() and
assert() or any other function.

Another way is to use echo() as an assignment to a dummy var:

function x() =
let(
assign_var1 = 1,
_ = echo(assign_var1),
assign_var2 = 2,
__ = echo(assign_var2)
)
[ assign_var1, assign_var2 ];

And yet a third way is a set of logging assignment functions from
openscad_logging 1 (though essentially that's just a lot of syntactic
sugar with extra stuff thrown around it, wrapped around the other two
methods).

On Sat, Oct 28, 2023 at 9:08 AM Nathan Sokalski njsokalski@hotmail.com
wrote:

Most people (including me) use echo statements primarily for debugging
purposes. This is usually pretty easy, except for inside a function.
Because a function is basically an assignment (it uses the = operator), I'm
not sure what the best way to debug a complex function is (for example,
determining the value that gets assigned to a variable in the let()). Any
ideas?

Nathan Sokalski
njsokalski@hotmail.com


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

I've done it a few ways: function x() = let( assign_var1 = 1, assign_var2 = 2 ) echo( assign_var1, assign_var2 ) [ assign_var1, assign_var2 ]; ...treats the echo() as an inline part of the function, just like let() and assert() or any other function. Another way is to use echo() as an assignment to a dummy var: function x() = let( assign_var1 = 1, _ = echo(assign_var1), assign_var2 = 2, __ = echo(assign_var2) ) [ assign_var1, assign_var2 ]; And yet a third way is a set of logging assignment functions from openscad_logging [1] (though essentially that's just a lot of syntactic sugar with extra stuff thrown around it, wrapped around the other two methods). [1]: https://github.com/jon-gilbert/openscad_logging On Sat, Oct 28, 2023 at 9:08 AM Nathan Sokalski <njsokalski@hotmail.com> wrote: > Most people (including me) use echo statements primarily for debugging > purposes. This is usually pretty easy, except for inside a function. > Because a function is basically an assignment (it uses the = operator), I'm > not sure what the best way to debug a complex function is (for example, > determining the value that gets assigned to a variable in the let()). Any > ideas? > > Nathan Sokalski > njsokalski@hotmail.com > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > -- - Jon Gilbert jong@jong.org / jgilbertsjc@gmail.com
JB
Jordan Brown
Sat, Oct 28, 2023 5:24 PM

On 10/28/2023 9:07 AM, Nathan Sokalski wrote:

Most people (including me) use echo statements primarily for debugging
purposes. This is usually pretty easy, except for inside a function.
Because a function is basically an assignment (it uses the =
operator), I'm not sure what the best way to debug a complex function
is (for example, determining the value that gets assigned to a
variable in the let()). Any ideas?

There are three ... let's call them pseudo-functions ... available in
expressions:  let, assert, and echo.

All of them are function-like constructs that you can put in front of
any expression (which includes subexpressions inside a larger expression).

Let, as you know, assigns values to variables for use in the following
expression.

Echo, like the echo module, prints values onto the console.

Assert, like the assert module, aborts the program if a condition is not
met.

Here's a contrived example showing all of them.

function div(a, b) =
    echo(a=a, b=b)
    assert(b != 0)
    let(quotient=a/b)
    echo(quotient=quotient)
    quotient;
echo(div(81,3));
echo(div(1,0));

Here's the output:

ECHO: a = 81, b = 3
ECHO: quotient = 27
ECHO: 27
ECHO: a = 1, b = 0
ERROR: Assertion '(b != 0)' failed in file OpenSCAD-2021.01, line 3
TRACE: called by 'div' in file OpenSCAD-2021.01, line 8
TRACE: called by 'echo' in file OpenSCAD-2021.01, line 8 

Documentation:

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Echo_function
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Using_assertions_in_function
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#let

(Though I would say that "let" is misplaced, in putting it alongside
normal functions.)

On 10/28/2023 9:07 AM, Nathan Sokalski wrote: > Most people (including me) use echo statements primarily for debugging > purposes. This is usually pretty easy, except for inside a function. > Because a function is basically an assignment (it uses the = > operator), I'm not sure what the best way to debug a complex function > is (for example, determining the value that gets assigned to a > variable in the let()). Any ideas? There are three ... let's call them pseudo-functions ... available in expressions:  let, assert, and echo. All of them are function-like constructs that you can put in front of any expression (which includes subexpressions inside a larger expression). Let, as you know, assigns values to variables for use in the following expression. Echo, like the echo module, prints values onto the console. Assert, like the assert module, aborts the program if a condition is not met. Here's a contrived example showing all of them. function div(a, b) = echo(a=a, b=b) assert(b != 0) let(quotient=a/b) echo(quotient=quotient) quotient; echo(div(81,3)); echo(div(1,0)); Here's the output: ECHO: a = 81, b = 3 ECHO: quotient = 27 ECHO: 27 ECHO: a = 1, b = 0 ERROR: Assertion '(b != 0)' failed in file OpenSCAD-2021.01, line 3 TRACE: called by 'div' in file OpenSCAD-2021.01, line 8 TRACE: called by 'echo' in file OpenSCAD-2021.01, line 8  Documentation: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Echo_function https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Using_assertions_in_function https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#let (Though I would say that "let" is misplaced, in putting it alongside normal functions.)
NH
nop head
Sat, Oct 28, 2023 5:59 PM

In my library l have a function called echoit which echoes the value passed
to and also returns it. So l can wrap it around any expression or just a
part of an expression to see the value without affecting it.

On Sat, 28 Oct 2023, 18:25 Jordan Brown, openscad@jordan.maileater.net
wrote:

On 10/28/2023 9:07 AM, Nathan Sokalski wrote:

Most people (including me) use echo statements primarily for debugging
purposes. This is usually pretty easy, except for inside a function.
Because a function is basically an assignment (it uses the = operator), I'm
not sure what the best way to debug a complex function is (for example,
determining the value that gets assigned to a variable in the let()). Any
ideas?

There are three ... let's call them pseudo-functions ... available in
expressions:  let, assert, and echo.

All of them are function-like constructs that you can put in front of any
expression (which includes subexpressions inside a larger expression).

Let, as you know, assigns values to variables for use in the following
expression.

Echo, like the echo module, prints values onto the console.

Assert, like the assert module, aborts the program if a condition is not
met.

Here's a contrived example showing all of them.

function div(a, b) =
echo(a=a, b=b)
assert(b != 0)
let(quotient=a/b)
echo(quotient=quotient)
quotient;
echo(div(81,3));
echo(div(1,0));

Here's the output:

ECHO: a = 81, b = 3
ECHO: quotient = 27
ECHO: 27
ECHO: a = 1, b = 0
ERROR: Assertion '(b != 0)' failed in file OpenSCAD-2021.01, line 3
TRACE: called by 'div' in file OpenSCAD-2021.01, line 8
TRACE: called by 'echo' in file OpenSCAD-2021.01, line 8

Documentation:

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Echo_function

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Using_assertions_in_function

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#let

(Though I would say that "let" is misplaced, in putting it alongside
normal functions.)


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

In my library l have a function called echoit which echoes the value passed to and also returns it. So l can wrap it around any expression or just a part of an expression to see the value without affecting it. On Sat, 28 Oct 2023, 18:25 Jordan Brown, <openscad@jordan.maileater.net> wrote: > On 10/28/2023 9:07 AM, Nathan Sokalski wrote: > > Most people (including me) use echo statements primarily for debugging > purposes. This is usually pretty easy, except for inside a function. > Because a function is basically an assignment (it uses the = operator), I'm > not sure what the best way to debug a complex function is (for example, > determining the value that gets assigned to a variable in the let()). Any > ideas? > > > There are three ... let's call them pseudo-functions ... available in > expressions: let, assert, and echo. > > All of them are function-like constructs that you can put in front of any > expression (which includes subexpressions inside a larger expression). > > Let, as you know, assigns values to variables for use in the following > expression. > > Echo, like the echo module, prints values onto the console. > > Assert, like the assert module, aborts the program if a condition is not > met. > > > Here's a contrived example showing all of them. > > function div(a, b) = > echo(a=a, b=b) > assert(b != 0) > let(quotient=a/b) > echo(quotient=quotient) > quotient; > echo(div(81,3)); > echo(div(1,0)); > > Here's the output: > > ECHO: a = 81, b = 3 > ECHO: quotient = 27 > ECHO: 27 > ECHO: a = 1, b = 0 > ERROR: Assertion '(b != 0)' failed in file OpenSCAD-2021.01, line 3 > TRACE: called by 'div' in file OpenSCAD-2021.01, line 8 > TRACE: called by 'echo' in file OpenSCAD-2021.01, line 8 > > > Documentation: > > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Echo_function > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Using_assertions_in_function > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#let > > (Though I would say that "let" is misplaced, in putting it alongside > normal functions.) > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
NH
nop head
Sat, Oct 28, 2023 7:22 PM

Here is the function:

function echoit(x) = echo(x) x;

On Sat, 28 Oct 2023 at 18:59, nop head nop.head@gmail.com wrote:

In my library l have a function called echoit which echoes the value
passed to and also returns it. So l can wrap it around any expression or
just a part of an expression to see the value without affecting it.

On Sat, 28 Oct 2023, 18:25 Jordan Brown, openscad@jordan.maileater.net
wrote:

On 10/28/2023 9:07 AM, Nathan Sokalski wrote:

Most people (including me) use echo statements primarily for debugging
purposes. This is usually pretty easy, except for inside a function.
Because a function is basically an assignment (it uses the = operator), I'm
not sure what the best way to debug a complex function is (for example,
determining the value that gets assigned to a variable in the let()). Any
ideas?

There are three ... let's call them pseudo-functions ... available in
expressions:  let, assert, and echo.

All of them are function-like constructs that you can put in front of any
expression (which includes subexpressions inside a larger expression).

Let, as you know, assigns values to variables for use in the following
expression.

Echo, like the echo module, prints values onto the console.

Assert, like the assert module, aborts the program if a condition is not
met.

Here's a contrived example showing all of them.

function div(a, b) =
echo(a=a, b=b)
assert(b != 0)
let(quotient=a/b)
echo(quotient=quotient)
quotient;
echo(div(81,3));
echo(div(1,0));

Here's the output:

ECHO: a = 81, b = 3
ECHO: quotient = 27
ECHO: 27
ECHO: a = 1, b = 0
ERROR: Assertion '(b != 0)' failed in file OpenSCAD-2021.01, line 3
TRACE: called by 'div' in file OpenSCAD-2021.01, line 8
TRACE: called by 'echo' in file OpenSCAD-2021.01, line 8

Documentation:

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Echo_function

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Using_assertions_in_function

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#let

(Though I would say that "let" is misplaced, in putting it alongside
normal functions.)


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

Here is the function: function echoit(x) = echo(x) x; On Sat, 28 Oct 2023 at 18:59, nop head <nop.head@gmail.com> wrote: > In my library l have a function called echoit which echoes the value > passed to and also returns it. So l can wrap it around any expression or > just a part of an expression to see the value without affecting it. > > On Sat, 28 Oct 2023, 18:25 Jordan Brown, <openscad@jordan.maileater.net> > wrote: > >> On 10/28/2023 9:07 AM, Nathan Sokalski wrote: >> >> Most people (including me) use echo statements primarily for debugging >> purposes. This is usually pretty easy, except for inside a function. >> Because a function is basically an assignment (it uses the = operator), I'm >> not sure what the best way to debug a complex function is (for example, >> determining the value that gets assigned to a variable in the let()). Any >> ideas? >> >> >> There are three ... let's call them pseudo-functions ... available in >> expressions: let, assert, and echo. >> >> All of them are function-like constructs that you can put in front of any >> expression (which includes subexpressions inside a larger expression). >> >> Let, as you know, assigns values to variables for use in the following >> expression. >> >> Echo, like the echo module, prints values onto the console. >> >> Assert, like the assert module, aborts the program if a condition is not >> met. >> >> >> Here's a contrived example showing all of them. >> >> function div(a, b) = >> echo(a=a, b=b) >> assert(b != 0) >> let(quotient=a/b) >> echo(quotient=quotient) >> quotient; >> echo(div(81,3)); >> echo(div(1,0)); >> >> Here's the output: >> >> ECHO: a = 81, b = 3 >> ECHO: quotient = 27 >> ECHO: 27 >> ECHO: a = 1, b = 0 >> ERROR: Assertion '(b != 0)' failed in file OpenSCAD-2021.01, line 3 >> TRACE: called by 'div' in file OpenSCAD-2021.01, line 8 >> TRACE: called by 'echo' in file OpenSCAD-2021.01, line 8 >> >> >> Documentation: >> >> >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Echo_function >> >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Using_assertions_in_function >> >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#let >> >> (Though I would say that "let" is misplaced, in putting it alongside >> normal functions.) >> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> >