variable behavior in functions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

variable behavior in functions

Parkinbot
Is this a bug or new feature?

echo(f());  // ECHO: 2
echo(q());  // ECHO: 1  and a warning

function f(a=0) =
let(a=a+1)
let(a=a+1)
a;

function q(a=0) =
let(a=a+1, a=a+1)
a;

As it is not semantically equivalent it should give an error and no warning.
I am using 2019.01.24.ci1256



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: variable behavior in functions

MichaelPFrey
On 04.02.19 15:34, Parkinbot wrote:
> Is this a bug or new feature?

I have no opinion on how openscad behaves in that case.

It is hard to separate intentional design, accidental consistency (due
to gnu bison, that happens), accidental inconsistency, quirks and
workarounds from each other.

(especially when keeping backward compatibility in mind)

Given how openscad2015 (and the development snapshot) behave, the
warning in the development snapshot is in my opinion correct and useful.

On 04.02.19 15:34, Parkinbot wrote:
> As it is not semantically equivalent it should give an error and no warning.

OpenSCAD is inconsistent when it comes to warnings and errors.


Part of the reason is, that OpenSCAD continuous on many errors (as if
they are just warnings).

There is no clear cut difference between errors and warnings.

Errors sometimes stop evaluation/geometry creation, but not always.

Warnings never stop evaluation/geometry creation (at least not directly).


Part of the reason for WARNING is, that I want to avoid breaking existing.

OpenSCAD users tend to be very creative in using the language and for
many years, the language had not that many warning/errors and silently
continued. A lot of designs use old language quirks like this one. (yes,
the assignment is ignored - but it is still there and changing/fixing
that quirk would break that specific design. Have fun finding that, when
it is deeply buried in a library)

Even my own OpenSCAD design regularly trigger warnings I my self have
implemented and it is sometimes a puzzle to find the cause. (even with
stack trace, linenumber and filename - things that are pretty recently
added to the errors and warnings)


in order to not break existing designs, but still pushing openscad into
a more beginner friendly direction (by warning about potential issues),
I simply add warnings. The user can still open, preview and render old
designs that use old bugs/quirks/inconsistencies as openscad still
behaves the same, except outputting a warning.


This off-course will lead some user to simply ignore warnings, but the
new feature "hardwarning" combined with "stack trace" will nudge people
towards writing code that does not normally generate warnings, so that
they can benefit from the stack trace if they unintentionally trigger a
warning.


We can discuss what is a warning and what is an error - but at the same
time:

Some people will jump from openscad2015-03-3 directly to the next release.

I do not want that the first impression of that release is, that various
designs are now broken and spew errors over errors, instead of a render
or preview.

The much better strategy is to add lots of warnings to the next release
and then to change some of those warnings into errors in the release
after that. That way we provide a migration path, by having one release
that behaves close to openscad2015, but warns about issues that we might
change in the future releases.


Sorry for not giving a clear cut answer, but I hope that my responds at
least explains a few of the ideas behind certain decisions.


With kind regards,

Michael Frey


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: variable behavior in functions

Parkinbot
I could personally pretty well live with

echo(f());  // ECHO: 2
function f(a=0) =
let(a=a+1)
let(a=a+1)
a;

as well as OpenSCAD would love to see a correct result, when trying to sum
up something like this:

echo(sum([1,3,4]));  // ECHO: [1,3,4]
function sum(A) = [let(b=0) for(i=A) let(b=b+i) b];

Since the same construction in a module also leads to a warning, it would be
consequent to also give a warning for f().

foo();
module foo(a=0)
{
    a = a+1;
    a = a+1;  // warning
  echo(a);     // 1
}







--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: variable behavior in functions

MichaelAtOz
Administrator
In reply to this post by Parkinbot
Parkinbot wrote

> Is this a bug or new feature?
>
> echo(f());  // ECHO: 2
> echo(q());  // ECHO: 1  and a warning
>
> function f(a=0) =
> let(a=a+1)
> let(a=a+1)
> a;
>
> function q(a=0) =
> let(a=a+1, a=a+1)
> a;
>
> As it is not semantically equivalent it should give an error and no
> warning.
> I am using 2019.01.24.ci1256

scope <https://en.wikipedia.org/wiki/Scope_%28computer_science%29>  
f() the second let() creates another scope, so you have a=0|a'=a+1|a''=a'+1
-> 2
g() the two a's are at the same scope level, a=0|a'=a+1;a'=a'+1 -> warning





-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: variable behavior in functions

Parkinbot
MichaelAtOz wrote
> f() the second let() creates another scope, so you have
> a=0|a'=a+1|a''=a'+1
> -> 2
> g() the two a's are at the same scope level, a=0|a'=a+1;a'=a'+1 -> warning

That makes sense. The module counterpart seems to be:

foo(1);  

module foo(a)
  let(a=a+1)  
  let(a=a+1)  
  echo(a);   // ECHO: 3

I wouldn't have expected this syntax to work in a module  ...



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org