Page 1 of 1

Allow expressions in a Switch block

Posted: Thu Jun 09, 2022 12:28 pm
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

Re: Allow expressions in a Switch block

Posted: Thu Jun 09, 2022 1:23 pm
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

Re: Allow expressions in a Switch block

Posted: Thu Jun 09, 2022 3:03 pm
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.

Re: Allow expressions in a Switch block

Posted: Thu Jun 09, 2022 4:56 pm
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