$name special variable?

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

$name special variable?

kintel
Administrator
Hi all,

Cross-posted from https://github.com/openscad/openscad/issues/339:

When developing libraries, it's common to include some test code in the global scope of the library. When doing this, opening the file as the main OpenSCAD file will execute the tests. As will include <mylibrary.scad>, whileuse <mylibrary.scad> will ignore the global instances of the file.

The include vs. use seems to be a bit confusing to some.
Also, the Thingiverse Customizer application being pretty popular, forces people into instantiating modules in the global scope.

To remedy this, and to make it possible for libraries to co-exist with Customizer objects, I'm considering adding a new special variable, e.g. $name, which will contain the current library filename of the file being evaluated. If you're evaluating the main file, $name == "main", otherwise, $name == filename.scad (without path).

Doing this, it would be possible to create libraries in somewhat the same way as Python modules:

module mylibrary(param) { ... }

// Customizer parameters

// choose parameter
MyParam = 1; // [0:Shape1, 1:Shape2, 2:Shape3]
mylibrary(MyParam);


if ($name == "main") {
  <library test code here>
}

The whole customizer stuff really should be standardized better, but it somehow exists and is used by numerous people, so we can only hope to standardize it next time around.

Any thoughts?

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

signature.asc (210 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: $name special variable?

Triffid Hunter
hm, checking strings smells a bit cumbersome to me just to see if we're top level or not.

perhaps a special block that's removed from includes and uses, like test() { } so we have a local scope in which to put any required variables?

or perhaps something more direct like $depth which == 0 for main, and 1 for first level include/use, etc ?

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: $name special variable?

nophead
It is only the same as Python though, so familiar to some of us:

if __name__ == '__main__':




On 27 April 2013 07:29, Triffid Hunter <[hidden email]> wrote:
hm, checking strings smells a bit cumbersome to me just to see if we're top level or not.

perhaps a special block that's removed from includes and uses, like test() { } so we have a local scope in which to put any required variables?

or perhaps something more direct like $depth which == 0 for main, and 1 for first level include/use, etc ?

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: $name special variable?

Alan Cox
In reply to this post by kintel
On Sat, 27 Apr 2013 01:30:21 -0400
Marius Kintel <[hidden email]> wrote:

> Hi all,
>
> Cross-posted from https://github.com/openscad/openscad/issues/339:
>
> When developing libraries, it's common to include some test code in the global scope of the library. When doing this, opening the file as the main OpenSCAD file will execute the tests. As will include <mylibrary.scad>, whileuse <mylibrary.scad> will ignore the global instances of the file.
>
> The include vs. use seems to be a bit confusing to some.
> Also, the Thingiverse Customizer application being pretty popular, forces people into instantiating modules in the global scope.
>
> To remedy this, and to make it possible for libraries to co-exist with Customizer objects, I'm considering adding a new special variable, e.g. $name, which will contain the current library filename of the file being evaluated. If you're evaluating the main file, $name == "main", otherwise, $name == filename.scad (without path).

Is $name the short name or long name in windows, is it case dependant, if
it is case dependant then in what locale is the case evaluation
performed ? How does it map between utf-8 file names and ucs-2 windows
names ? In the Unix case which name is used if the file is opened via a
symbolic link or what if it has no name - eg compiling a pipeline in
future ? What if the file is called "main" ?

The file name strikes me as a completely crazy way of doing it.

Breaking openscad because the customizers suck strikes me as the tail
wagging the dog. Fix the dog and the tail will have to follow.

use/include is certainly an oddity but a very useful one.

$name doesn't seem to be the right way to handle this. If the question is
"is this the top level module" then surely the test should be that not
some name mangling hackery ? or should at least be keyed to a stable
language feature itself

eg replace $name = filename with $name = module name and "" can mean
"none"

so

module wibble() {
        echo($name);
}

would say "wibble"

All this however has other problems. What does it mean to "include"
something which then contains code contradicting the intent of the
include. With the main hack or with another test you've now got two
language semantics that contradict fighting each other for precedence.

Customisers almost want some kind of grouping of variables really

arg.a = foo
arg.b = bar

draw_wotsit(arg)

and that I guess leads on to other language questions like whether you
want to be able to group sets of modules together with a 'library' global
namespace - eg by allowing heirarchical modules so you could deal with
all the include scoping.

A vastly bigger problem IMHO with libraries and use/include is that you
can't do this

module foo() {
        // Constants and internal private settings for the library
        a = 1;
        b  = 2;

        module bar() {
                x = 3;
                echo($a);
        }

        bar();
}

foo();

which means you can't have a library global constant space even though
you can have a heirarchical module naming and class private modules.

Alan
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: $name special variable?

kintel
Administrator
On 2013-04-27, at 07:01 , Alan Cox wrote:
>
> The file name strikes me as a completely crazy way of doing it.
>
Point taken :)

Having $name evaluate to the name of the current module definition would make sense, and keep it empty (or undefined) in the global scope.

I don't think this would conflict with use/include:
o "include" includes all the included code into the current scope
o "use" makes functions and modules available to the current scope

So with the new $name suggestion, include'ing a file will execute the body of the included file without changing $name, while use'ing a file will not execute anything.

I do agree that customizer-like software really should use well-defined language constructs and rely on officially supported mechanisms. I'd like to make that happen.

> A vastly bigger problem IMHO with libraries and use/include is that you
> can't do this
>
> module foo() {
> // Constants and internal private settings for the library
> a = 1;
> b  = 2;
>
> module bar() {
> x = 3;
> echo($a);
> }
>
> bar();
> }
>
> foo();
>
Actually, this has always been possible. You just cannot use '$a' to refer to a variable named 'a'.

-Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: $name special variable?

Felipe Sanches
My way of dealing with the issue has been a design pattern I've come up with and that I've been adopting for a while in my projects:

I create a submodule_name.scad file with the modules and functions for a specific part, let's say... lm8uu_bearing.scad
Then I create a header file with the global declarations such as lm8uu_length=24 and lm8uu_diameter=15. This file is called lm8uu_bearing.h

The first line of lm8uu_bearing.h is use<lm8uu_bearing.scad>;
That way, when I need to have lm8uu linear bearing in my design I simply write include<lm8uu_bearing.h>;
And if I open lm8uu_bearing.scad it will render the model for me because at the end I have instantiations of the model such as lm8uu(); and these are ignored by the use statement, but executed when I open the file directly.

I think that way I can have my scripts well organized and it's also pretty much familiar to people who've worked with other programming languages before.

happy hacking,
Felipe Sanches

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566