I really like the Switch block, very handy to be able to select an action from a number of values.
Unfortunately, the Swich is controlled only by a constant
I think that in most cases you don't have such a clear situation, but you have to convert some expression or condition into a constant to be able to use the Switch.
It would be great if we could use regular expressions in a Switch select field; that would open up a lot of smart use cases!
Jan Didden
Allow expressions in a Switch block
-
- Posts: 82
- http://meble-kuchenne.info.pl
- Joined: Thu Dec 17, 2020 3:16 pm
- Has thanked: 20 times
- Been thanked: 12 times
-
- Valued Contributor
- Posts: 1453
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 707 times
Re: Allow expressions in a Switch block
Hi Jan,
Switch does only take a constant value in Flowcode (and the underlying C).
There is a way around it using an array of pointers to macros - which is quick and relatively easy to use (but requires a smidgen of C to set up and call the macros.
I used this to create an Arduino client and it works well - can let you have some sample code if it is useful.
Martin
Switch does only take a constant value in Flowcode (and the underlying C).
There is a way around it using an array of pointers to macros - which is quick and relatively easy to use (but requires a smidgen of C to set up and call the macros.
I used this to create an Arduino client and it works well - can let you have some sample code if it is useful.
Martin
-
- Matrix Staff
- Posts: 1465
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 204 times
- Been thanked: 347 times
Re: Allow expressions in a Switch block
I did consider extending the 'switch' statement in Flowcode to allow non-constant values, but I decided against it. For a few reasons:
- It is essentially analogous to the "switch/case" statement in C, which is restricted to const values. And because part of Flowcode's aim is to help people learn C, I decided to keep it consistent.
- Having expressions would mean multiple branches could be 'true', which could lead to confusion.
- You can achieve a similar result with nested decision icons.
-
- Posts: 82
- Joined: Thu Dec 17, 2020 3:16 pm
- Has thanked: 20 times
- Been thanked: 12 times
Re: Allow expressions in a Switch block
Thanks Steve, I hadn't thought about your 2nd point which is important.
I did in fact use a nested decision tree instead.
@mnfisher yes I'd like a code sample, just out of curiosity!
Jan
I did in fact use a nested decision tree instead.
@mnfisher yes I'd like a code sample, just out of curiosity!
Jan