Allow expressions in a Switch block

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
Post Reply
jan.didden
Posts: 82
http://meble-kuchenne.info.pl
Joined: Thu Dec 17, 2020 3:16 pm
Has thanked: 20 times
Been thanked: 12 times

Allow expressions in a Switch block

Post by jan.didden »

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

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Allow expressions in a Switch block

Post by mnfisher »

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

Steve-Matrix
Matrix Staff
Posts: 1234
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Allow expressions in a Switch block

Post by Steve-Matrix »

I did consider extending the 'switch' statement in Flowcode to allow non-constant values, but I decided against it. For a few reasons:
  1. 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.
  2. Having expressions would mean multiple branches could be 'true', which could lead to confusion.
  3. You can achieve a similar result with nested decision icons.

jan.didden
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

Post by jan.didden »

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

Post Reply