Discussion:
[OpenSCAD] Module return
ashuan
8 years ago
Permalink
Hello alls,

I'm a newbie to OpenScad but not to programing language.
I have a blocking point about module return.
I have a look about this forum without finded answer.

I my preferated world, i wish have a syntaxe like this :

module <name> (<parameter1>, <parameter2>, ..., <parameterN>) {
<instruction1>;
<instruction2>;
...
<instructionN>;
<name> = <return value>; // << or return <return value> or result <return
value> or ...
}

an example that is useless if it is not to illustrate my purpose :
module inList(searchText, list)
{
resultat = false;
if (len(search(searchText, list)== 0))
{
resultat = true;
}
inList = resultat;
}

Could you help me, please ?




--
Sent from: http://forum.openscad.org/
Gadgetmind
8 years ago
Permalink
Post by ashuan
<name> = <return value>; // << or return <return value> or result <return
value> or ...
All OpenSCAD modules can "return" is geometry.

OpenSCAD functions can return values including vectors and strings, but
coding inside functions is rather different to in modules. You'll
probably want to make heavy use of the let () element, and either the ?
: method of choosing what to return or the new ifelse described here.

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/WIP#if.2Felse

function inList(searchText, list) =
let ( result = search(searchText, list, 0))
len(result) == 0 ? true : result;


echo ( inList ("a", "abcdabcd") );

Note that the vector returned from search doesn't have the length you might expect if the result isn't found with num_returns_per_match=0, so the example isn't great.
ashuan
8 years ago
Permalink
Thanks for this answer.




--
Sent from: http://forum.openscad.org/
ashuan
8 years ago
Permalink
Thanks for this answer



--
Sent from: http://forum.openscad.org/
ashuan
8 years ago
Permalink
I'm use customizer plugin, it's very nice.



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

Eric Buijs
8 years ago
Permalink
OpenSCAD is a functional programming language and contrary to imperative
programming you can't reassign a new value to a variable. Something I
struggled with in the beginning. For better understanding I wrote an short
article on this
http://eribuijs.blogspot.nl/2017/09/openscad-functional-programming.html
<http://eribuijs.blogspot.nl/2017/09/openscad-functional-programming.html>
. Hopefully it's useful.

In OpenSCAD a *module* is generally used to define objects but *functions*
operate on values an return values. So you need a function. Luckily OpenSCAD
already has a built-in function search that we can use in your case. Below
is an example that may be applicable to your situation.

lst = "abcdabcd";
function st_search (st,lst) = (search(st,lst) != []) ? true : false;
echo(st_search("a",lst));



--
Sent from: http://forum.openscad.org/
ashuan
8 years ago
Permalink
Thanks for this answer



--
Sent from: http://forum.openscad.org/
nop head
8 years ago
Permalink
Modules create or transform geometry, they don't return values and can't be
used in expressions. You need a function for that.

function inList(searchText, list) = len(search(searchText, list)) == 0;
...
Tim V. Shaporev
8 years ago
Permalink
Wow! You know Boolean logic!
As was really desperate looking on "? true : false"
:-|
...
ashuan
8 years ago
Permalink
Thanks all for your explanation.

I've understand now the positionnement logic of module.

I'll try your 3 examples without success.

For understanding, i've this call :

// variables
modulesActifs = ["verin", "title"]; // vague

// affiche un système de vague coloré
if (inList("vague", modulesActifs))
{
...





--
Sent from: http://forum.openscad.org/
Continue reading on narkive:
Loading...