Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Featurescript language - testing non booleans?
traveler_hauptman
Member, OS Professional, Mentor, Developers Posts: 419 PRO
This is not really a feature request because it depends a lot on the intent of the FS language designers.
It can be really useful if undefined tests as false and everything other non-boolean value tests true.
It can be really useful if undefined tests as false and everything other non-boolean value tests true.
var result = maybe_do_something(); //Returns undefined if unable to do somethingIt can be useful if && and || can take non-boolean operands.
if(!result){/*Handle missing value*/}
myCondition && nonBooleanExpression();
approach1() || approach2();
0
Comments
if (result != undefined) {}
makes it clear that result is not intended to be a boolean, and that undefined is the intended error state (someone looking at your code sees that immediately). In this specific case an equally worthwhile protocol can be to have maybe_do_something() throw an error in the empty case, and use try/catch, rather than an if statement, when calling it. This allows the function return and the result variable to be have checked types.myCondition : nonBooleanExpression() ? undefined
makes it clear what the return value will be when myCondition fails.try { approach1(); } catch { approach2(); }
is certainly more more verbose than the boolean, but also makes it obvious when each statement gets run and what the result is in each case.For myself though, I'll be trying to bang out a quick bit of automation in featurescript a couple times a year and will have just been using who-knows-what language and idiom. I have done multi-week projects in C++, Ruby, Python, Bash, Javascript, and Java in the last six months and I forget the syntax details of every one when I put it down (except C ) .
I've never been angry at a programming language for being too expressive or flexible.
That doesn't make me want the shortcut any less though.
Thinking about undefined reminds me how uneasy I get with returning undefined when a map key is missing. I always get stuck wondering if I need to differentiate, and test for, a missing key vs an existing key with a value of undefined. This trips me up everytime I touch Lua for instance where maps are such an integral part of the programming idiom. JS has the same behaviour but I don't use JS in a way that makes me confront this.
...now I'm trying to remember which languages treat the undefined/nil value as true. I know I've seen it in other places but can't find it now.