T
Trygon
Sat, Feb 13, 2016 3:39 PM
Hi All,
I want to determine what type of data is passed into a module through a
single variable, i.e. whether it is a number, string or vector:
foo(5);
foo("hello");
foo([0,1,2]);
I came up with the following:
module foo(a){
if(a==undef) echo("undefined");
else if(len(a)==0) echo("zero length string or empty vector");
else if(a[0]==undef) echo("number");
else if(a[0]+0==undef) echo("string");
else echo("vector");
}
Questions:
- Is there a better way?
- Is this the only way (at present)?
- Does this method use undocumented language "features" that may change in
future releases? (... and thus break this code)
Thanks,
Trygon
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Hi All,
I want to determine what type of data is passed into a module through a
single variable, i.e. whether it is a number, string or vector:
foo(5);
foo("hello");
foo([0,1,2]);
I came up with the following:
module foo(a){
if(a==undef) echo("undefined");
else if(len(a)==0) echo("zero length string or empty vector");
else if(a[0]==undef) echo("number");
else if(a[0]+0==undef) echo("string");
else echo("vector");
}
Questions:
1) Is there a better way?
2) Is this the only way (at present)?
2) Does this method use undocumented language "features" that may change in
future releases? (... and thus break this code)
Thanks,
Trygon
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
TP
Torsten Paul
Sat, Feb 13, 2016 3:58 PM
On 02/13/2016 04:39 PM, Trygon wrote:
I want to determine what type of data is passed into a module through a
single variable, i.e. whether it is a number, string or vector:
Just curious... why is that needed?
Questions:
- Is there a better way?
No, I don't think there's a good way right now, as there is no "official"
way to determine the type.
Well, in my opinion the better way would be to not use that type
detection at all, but I guess that answer was not in scope of the
question...
- Is this the only way (at present)?
There might be variations, but I'd tend to say yes.
- Does this method use undocumented language "features" that may
change in future releases? (... and thus break this code)
Yes. It tries to infer information based on assumptions that will
break easily by adding new features. Normally new features are not
a big deal as they can't be used in existing code (there are some
exceptions like naming clashes).
So depending on how this code is actually used, it may be very
prone to break with future releases.
It's also incomplete as it ignores ranges. Calling foo([3:4]) returns
"vector" (which basically shows the point I tried to make above).
ciao,
Torsten.
On 02/13/2016 04:39 PM, Trygon wrote:
> I want to determine what type of data is passed into a module through a
> single variable, i.e. whether it is a number, string or vector:
>
Just curious... why is that needed?
> Questions:
> 1) Is there a better way?
>
No, I don't think there's a good way right now, as there is no "official"
way to determine the type.
Well, in my opinion the better way would be to not use that type
detection at all, but I guess that answer was not in scope of the
question...
> 2) Is this the only way (at present)?
>
There might be variations, but I'd tend to say yes.
> 2) Does this method use undocumented language "features" that may
> change in future releases? (... and thus break this code)
>
Yes. It tries to infer information based on assumptions that will
break easily by adding new features. Normally new features are not
a big deal as they can't be used in existing code (there are some
exceptions like naming clashes).
So depending on how this code is actually used, it may be very
prone to break with future releases.
It's also incomplete as it ignores ranges. Calling foo([3:4]) returns
"vector" (which basically shows the point I tried to make above).
ciao,
Torsten.
T
Trygon
Sat, Feb 13, 2016 5:41 PM
Thanks Torsten, I didn't know if I had missed something obvious.
"Just curious... why is that needed?":
I have an idea for a module that will produce a specific object from a
family of related objects based on a description passed to it. The object
description "language" is a vector containing a variable length sequence of
numbers, vectors and strings. The syntax I have arrived at for this simple
description language does not allow the "interpreter" to determine in
advance what the next element will be, hence the need to determine the data
type of a variable.
This probably shouldn't be scripted in OpenSCAD... ;-)
Cheers,
Trygon
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16113.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Thanks Torsten, I didn't know if I had missed something obvious.
"Just curious... why is that needed?":
I have an idea for a module that will produce a specific object from a
family of related objects based on a description passed to it. The object
description "language" is a vector containing a variable length sequence of
numbers, vectors and strings. The syntax I have arrived at for this simple
description language does not allow the "interpreter" to determine in
advance what the next element will be, hence the need to determine the data
type of a variable.
This probably shouldn't be scripted in OpenSCAD... ;-)
Cheers,
Trygon
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16113.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
DM
doug moen
Sat, Feb 13, 2016 5:43 PM
There is no better way, right now. And yeah, lots of people write code like
this, based on my own observations.
If you write code like this, then it will break the feature being discussed
here:
https://github.com/openscad/openscad/issues/1574#issuecomment-177988896
That's okay, it just means we'll need a migration strategy.
In OpenSCAD2, I've dealt with this problem by providing functions that test
the type of a value, and return true or false. The OpenSCAD2 API doesn't
quite fit into OpenSCAD right now, so I won't show that, but based on that
design, I'd recommend we add the following functions to OpenSCAD:
- is_bool
- is_int
- is_num
- is_string
- is_list
In OpenSCAD2, ranges are not distinguishable from lists at the user level
(although the internal representation is different). [1:4] is
indistinguishable from [1,2,3,4], and you can even use range notation as
part of the extended list comprehension syntax that Torsten recently
implemented. Eg, [1,1:3,5,8] == [1,1,2,3,5,8]. That's why I didn't propose
an is_range test.
https://github.com/openscad/openscad/issues/1574#issuecomment-183660896
On 13 February 2016 at 10:39, Trygon db5765@outlook.com wrote:
Hi All,
I want to determine what type of data is passed into a module through a
single variable, i.e. whether it is a number, string or vector:
foo(5);
foo("hello");
foo([0,1,2]);
I came up with the following:
module foo(a){
if(a==undef) echo("undefined");
else if(len(a)==0) echo("zero length string or empty vector");
else if(a[0]==undef) echo("number");
else if(a[0]+0==undef) echo("string");
else echo("vector");
}
Questions:
- Is there a better way?
- Is this the only way (at present)?
- Does this method use undocumented language "features" that may change in
future releases? (... and thus break this code)
Thanks,
Trygon
--
View this message in context:
http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
There is no better way, right now. And yeah, lots of people write code like
this, based on my own observations.
If you write code like this, then it will break the feature being discussed
here:
https://github.com/openscad/openscad/issues/1574#issuecomment-177988896
That's okay, it just means we'll need a migration strategy.
In OpenSCAD2, I've dealt with this problem by providing functions that test
the type of a value, and return true or false. The OpenSCAD2 API doesn't
quite fit into OpenSCAD right now, so I won't show that, but based on that
design, I'd recommend we add the following functions to OpenSCAD:
* is_bool
* is_int
* is_num
* is_string
* is_list
In OpenSCAD2, ranges are not distinguishable from lists at the user level
(although the internal representation is different). [1:4] is
indistinguishable from [1,2,3,4], and you can even use range notation as
part of the extended list comprehension syntax that Torsten recently
implemented. Eg, [1,1:3,5,8] == [1,1,2,3,5,8]. That's why I didn't propose
an is_range test.
<https://github.com/openscad/openscad/issues/1574#issuecomment-183660896>
On 13 February 2016 at 10:39, Trygon <db5765@outlook.com> wrote:
> Hi All,
>
> I want to determine what type of data is passed into a module through a
> single variable, i.e. whether it is a number, string or vector:
>
> foo(5);
> foo("hello");
> foo([0,1,2]);
>
> I came up with the following:
>
> module foo(a){
> if(a==undef) echo("undefined");
> else if(len(a)==0) echo("zero length string or empty vector");
> else if(a[0]==undef) echo("number");
> else if(a[0]+0==undef) echo("string");
> else echo("vector");
> }
>
> Questions:
> 1) Is there a better way?
> 2) Is this the only way (at present)?
> 2) Does this method use undocumented language "features" that may change in
> future releases? (... and thus break this code)
>
> Thanks,
> Trygon
>
>
>
>
> --
> View this message in context:
> http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
>
TP
Torsten Paul
Sat, Feb 13, 2016 5:58 PM
On 02/13/2016 06:41 PM, Trygon wrote:
This probably shouldn't be scripted in OpenSCAD... ;-)
Well, as always, it's a good idea to consider the implications
and also trying to step back and consider other options.
If you have a clearly defined set of input values and a
single central implementation for the type check, you
should be ok-ish with the detection logic you have right
now.
In case OpenSCAD grows data introspection features later,
you can just swap out the current heuristics with the
official feature.
With OpenSCAD targeted as model description language,
it does lack a number of features needed for general
data processing, it's still interesting to see the
borders tested by clever usage of existing things :-).
ciao,
Torsten.
On 02/13/2016 06:41 PM, Trygon wrote:
> This probably shouldn't be scripted in OpenSCAD... ;-)
>
Well, as always, it's a good idea to consider the implications
and also trying to step back and consider other options.
If you have a clearly defined set of input values and a
single central implementation for the type check, you
should be ok-ish with the detection logic you have right
now.
In case OpenSCAD grows data introspection features later,
you can just swap out the current heuristics with the
official feature.
With OpenSCAD targeted as model description language,
it does lack a number of features needed for general
data processing, it's still interesting to see the
borders tested by clever usage of existing things :-).
ciao,
Torsten.
R
runsun
Sun, Feb 14, 2016 4:54 PM
@Trygon, type detection and casting have been crucial parts of my lib. Here
is how I did it:
function type(x)=
(
x==undef?undef
: floor(x)==x? "int"
: ( abs(x)+1>abs(x)?"float"
: str(x)==x?"str"
: str(x)=="false"||str(x)=="true"?"bool"
: (x[0]==x[0])&&len(x)!=undef? "arr" // range below doesn't have len
: let( s=str(x)
, s2= split(slice(s,1,-1)," : ")
)
s[0]=="[" && s[len(s)-1]=="]"
&& all( [ for(x=s2) isint(int(x)) ] )?"range"
:"unknown"
)
);
This passes all tests below:
type ( x )=str ( tested:18 ) (mode:12)
| 0> type([2, 3])= "arr"
| 1> type([])= "arr"
| 2> type(-4)= "int"
| 3> type(0)= "int"
| 4> type(0.3)= "float"
| 5> type(-0.3)= "float"
| 6> type(2e-6)= "float"
| 7> type(2e3)= "int"
| 8> type(2e-06)= "float"
| 9> type(2e+03)= "int"
| 10> type("a")= "str"
| 11> type("10")= "str"
| 12> type(true)= "bool"
| 13> type(false)= "bool"
| 14> type([1:2:10])= "range" // New 2015.9.10
| 15> type([1:1.5:10])= "range" // New 2015.9.10
| 16> type(undef)= undef
| 17> type(12345678901234500)= "int" // Bugfix 2015.11.17
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep , var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16126.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
@Trygon, type detection and casting have been crucial parts of my lib. Here
is how I did it:
> function type(x)=
> (
> x==undef?undef
> : floor(x)==x? "int"
> : ( abs(x)+1>abs(x)?"float"
> : str(x)==x?"str"
> : str(x)=="false"||str(x)=="true"?"bool"
> : (x[0]==x[0])&&len(x)!=undef? "arr" // range below doesn't have len
> : let( s=str(x)
> , s2= split(slice(s,1,-1)," : ")
> )
> s[0]=="[" && s[len(s)-1]=="]"
> && all( [ for(x=s2) isint(int(x)) ] )?"range"
> :"unknown"
> )
> );
This passes all tests below:
> type ( x )=str ( tested:18 ) (mode:12)
> ------------------------------------------------
> | 0> type([2, 3])= "arr"
> | 1> type([])= "arr"
> | 2> type(-4)= "int"
> | 3> type(0)= "int"
> | 4> type(0.3)= "float"
> | 5> type(-0.3)= "float"
> | 6> type(2e-6)= "float"
> | 7> type(2e3)= "int"
> | 8> type(2e-06)= "float"
> | 9> type(2e+03)= "int"
> | 10> type("a")= "str"
> | 11> type("10")= "str"
> | 12> type(true)= "bool"
> | 13> type(false)= "bool"
> | 14> type([1:2:10])= "range" // New 2015.9.10
> | 15> type([1:1.5:10])= "range" // New 2015.9.10
> | 16> type(undef)= undef
> | 17> type(12345678901234500)= "int" // Bugfix 2015.11.17
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep , var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16126.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
A
Adam
Sun, Feb 14, 2016 8:10 PM
"Guys"
First and foremost, thank you for your responses.
Michael, I am not posting here deliberately as I am getting flooded with emails that do not apply to my request. I am not deliberately irritating you. I am replying out of courtesy. How do I stop getting information about this primitive program where it does not apply to my original post?
As I said in my original post, I have no idea what language you are using and no idea how to code in it.
If you understand my dilema and are sympathetic and willing to help me out I would be greatly appreciative.
All I wanted to do was to import an InkScape file into OpenScad then covert it to a 3D image, a project which I found on "Instructables" and "Thingiverse." The author said it was simple and there were users who agreed. Now my email is being flooded with info about "For" loops and coding instructions which seemingly have nothing to do with my original project.
The method said that I was supposed to be able to convert the 2D image into a Scad file and import it into OpenScad. I have followed instructions faithfully and I cannot get InkScape to convert the file or OpenScad to import the image.
Now I am getting a lot of emails with code and I have no idea what the language is or how to use it inspite of the fact that I can code in 4 different flavors of BASIC and Fortran. As one who has no clue about the programming language I have no use for "For" Loops even though they are an integral part of Structured BASIC and I do not wish to learn a new programming language as I already am far too sidetracted from my original mission.
As for OpenScad "evolving," I have not seen this level of "throw back" programming since 1985. One well meaning individual even said that my OS looked like a very old version. It's Windows 10/64 on a quad phenom.
----- Original Message -----
From: runsunmailto:runsun@gmail.com
To: discuss@lists.openscad.orgmailto:discuss@lists.openscad.org
Sent: Sunday, February 14, 2016 10:54 AM
Subject: Re: [OpenSCAD] Determining what data type a variable is holding
@Trygon, type detection and casting have been crucial parts of my lib. Here
is how I did it:
function type(x)=
(
x==undef?undef
: floor(x)==x? "int"
: ( abs(x)+1>abs(x)?"float"
: str(x)==x?"str"
: str(x)=="false"||str(x)=="true"?"bool"
: (x[0]==x[0])&&len(x)!=undef? "arr" // range below doesn't have len
: let( s=str(x)
, s2= split(slice(s,1,-1)," : ")
)
s[0]=="[" && s[len(s)-1]=="]"
&& all( [ for(x=s2) isint(int(x)) ] )?"range"
:"unknown"
)
);
This passes all tests below:
type ( x )=str ( tested:18 ) (mode:12)
| 0> type([2, 3])= "arr"
| 1> type([])= "arr"
| 2> type(-4)= "int"
| 3> type(0)= "int"
| 4> type(0.3)= "float"
| 5> type(-0.3)= "float"
| 6> type(2e-6)= "float"
| 7> type(2e3)= "int"
| 8> type(2e-06)= "float"
| 9> type(2e+03)= "int"
| 10> type("a")= "str"
| 11> type("10")= "str"
| 12> type(true)= "bool"
| 13> type(false)= "bool"
| 14> type([1:2:10])= "range" // New 2015.9.10
| 15> type([1:1.5:10])= "range" // New 2015.9.10
| 16> type(undef)= undef
| 17> type(12345678901234500)= "int" // Bugfix 2015.11.17
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep , var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16126.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
"Guys"
First and foremost, thank you for your responses.
Michael, I am not posting here deliberately as I am getting flooded with emails that do not apply to my request. I am not deliberately irritating you. I am replying out of courtesy. How do I stop getting information about this primitive program where it does not apply to my original post?
As I said in my original post, I have no idea what language you are using and no idea how to code in it.
If you understand my dilema and are sympathetic and willing to help me out I would be greatly appreciative.
All I wanted to do was to import an InkScape file into OpenScad then covert it to a 3D image, a project which I found on "Instructables" and "Thingiverse." The author said it was simple and there were users who agreed. Now my email is being flooded with info about "For" loops and coding instructions which seemingly have nothing to do with my original project.
The method said that I was supposed to be able to convert the 2D image into a Scad file and import it into OpenScad. I have followed instructions faithfully and I cannot get InkScape to convert the file or OpenScad to import the image.
Now I am getting a lot of emails with code and I have no idea what the language is or how to use it inspite of the fact that I can code in 4 different flavors of BASIC and Fortran. As one who has no clue about the programming language I have no use for "For" Loops even though they are an integral part of Structured BASIC and I do not wish to learn a new programming language as I already am far too sidetracted from my original mission.
As for OpenScad "evolving," I have not seen this level of "throw back" programming since 1985. One well meaning individual even said that my OS looked like a very old version. It's Windows 10/64 on a quad phenom.
----- Original Message -----
From: runsun<mailto:runsun@gmail.com>
To: discuss@lists.openscad.org<mailto:discuss@lists.openscad.org>
Sent: Sunday, February 14, 2016 10:54 AM
Subject: Re: [OpenSCAD] Determining what data type a variable is holding
@Trygon, type detection and casting have been crucial parts of my lib. Here
is how I did it:
> function type(x)=
> (
> x==undef?undef
> : floor(x)==x? "int"
> : ( abs(x)+1>abs(x)?"float"
> : str(x)==x?"str"
> : str(x)=="false"||str(x)=="true"?"bool"
> : (x[0]==x[0])&&len(x)!=undef? "arr" // range below doesn't have len
> : let( s=str(x)
> , s2= split(slice(s,1,-1)," : ")
> )
> s[0]=="[" && s[len(s)-1]=="]"
> && all( [ for(x=s2) isint(int(x)) ] )?"range"
> :"unknown"
> )
> );
This passes all tests below:
> type ( x )=str ( tested:18 ) (mode:12)
> ------------------------------------------------
> | 0> type([2, 3])= "arr"
> | 1> type([])= "arr"
> | 2> type(-4)= "int"
> | 3> type(0)= "int"
> | 4> type(0.3)= "float"
> | 5> type(-0.3)= "float"
> | 6> type(2e-6)= "float"
> | 7> type(2e3)= "int"
> | 8> type(2e-06)= "float"
> | 9> type(2e+03)= "int"
> | 10> type("a")= "str"
> | 11> type("10")= "str"
> | 12> type(true)= "bool"
> | 13> type(false)= "bool"
> | 14> type([1:2:10])= "range" // New 2015.9.10
> | 15> type([1:1.5:10])= "range" // New 2015.9.10
> | 16> type(undef)= undef
> | 17> type(12345678901234500)= "int" // Bugfix 2015.11.17
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep , var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16126.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
R
runsun
Sun, Feb 14, 2016 8:47 PM
I am getting flooded with emails that do not apply to my request. I am
not deliberately irritating you. I am replying out of courtesy. How do I
stop getting information about this primitive program where it does not
apply to my original post?
Obviously you've turned on the mail delivery in you account settings.
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep( 1 , 2 ), var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control , type
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16128.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
adamtimm1 wrote
> I am getting flooded with emails that do not apply to my request. I am
> not deliberately irritating you. I am replying out of courtesy. How do I
> stop getting information about this primitive program where it does not
> apply to my original post?
Obviously you've turned on the mail delivery in you account settings.
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep( 1 , 2 ), var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control , type
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16128.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Sun, Feb 14, 2016 9:10 PM
removed adam from the mailing list and will explain via PM.
Newly minted Admin - PM me if you need anything, or if I've done something stupid...
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!
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16129.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
removed adam from the mailing list and will explain via PM.
-----
Newly minted Admin - PM me if you need anything, or if I've done something stupid...
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!
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16129.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Mon, Feb 15, 2016 3:04 AM
function type(x)=
(
x==undef?undef
: floor(x)==x? "int"
: ( abs(x)+1>abs(x)?"float"
: str(x)==x?"str"
: str(x)=="false"||str(x)=="true"?"bool"
: (x[0]==x[0])&&len(x)!=undef? "arr" // range below doesn't have len
: let( s=str(x)
, s2= split(slice(s,1,-1)," : ")
)
s[0]=="[" && s[len(s)-1]=="]"
&& all( [ for(x=s2) isint(int(x)) ] )?"range"
:"unknown"
)
);
@runsun, ' isint(int(x))' I presume your int() function returns undef or nan
when x is not a number?
Newly minted Admin - PM me if you need anything, or if I've done something stupid...
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!
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16142.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
runsun wrote
>> function type(x)=
>> (
>> x==undef?undef
>> : floor(x)==x? "int"
>> : ( abs(x)+1>abs(x)?"float"
>> : str(x)==x?"str"
>> : str(x)=="false"||str(x)=="true"?"bool"
>> : (x[0]==x[0])&&len(x)!=undef? "arr" // range below doesn't have len
>> : let( s=str(x)
>> , s2= split(slice(s,1,-1)," : ")
>> )
>> s[0]=="[" && s[len(s)-1]=="]"
>> && all( [ for(x=s2) isint(int(x)) ] )?"range"
>> :"unknown"
>> )
>> );
@runsun, ' isint(int(x))' I presume your int() function returns undef or nan
when x is not a number?
-----
Newly minted Admin - PM me if you need anything, or if I've done something stupid...
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!
--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16142.html
Sent from the OpenSCAD mailing list archive at Nabble.com.