Writing Extensions

Mark Waddingham mark at livecode.com
Wed May 24 06:25:05 EDT 2017


On 2017-05-24 12:03, Mark Waddingham via use-livecode wrote:
> Perhaps a better model would be to use 'continue', rather than 'break':
> 
> switch yourName
>   case "Mark"
>     -- do something and finish
>   case "John"
>     continue
>   case "Jon"
>     -- do something else
> end switch
> 
> The idea being that 'continue' in a case, jumps to the next choice to
> check. This works particularly nicely when you don't have an
> expression, so the cases can be expressions as that essentially gives
> you a 'match' type construct with back-tracking (quite useful for
> processing hierarchical arrays and transforming them - a common
> operation in writing compilers, for example).

Okay, so that isn't quite right - for a switch-on-value switch, continue
would have to continue into the top the next case, ignoring the case 
value which is not quite the same as it would be in switch-on-case-expr 
switch.

Certainly a 'fallthrough' type keyword would work... However, stepping 
back and looking at the utility of switch. I'd say that in almost all 
cases you either want to:

   1) Map a single case to a block of code

   2) Map multiple cases to a block of code

The use-case which causes subtle bugs and errors is where one case 
requires a 'bit more code' before the others (where code, fallthrough, 
code is *not* erroneous but intended) - also it is a rarely used pattern 
in C (because it is difficult to maintain).

Taking this into account, then perhaps a better solution would be 
('choose' used here for the sake of argument):

   choose tValue
     when 1
     when 2
     when 3
       -- executes if tValue is 1, 2 or 3
       -- never falls through

     when 4
       -- never falls through

     default
   end choose

This caters for both cases (1) and (2) and is unambiguous.

As I said before, the lack of switch in LCB isn't because we don't want 
a construct like that; just that we'd like one which doesn't have the 
issues of C-style switch. (The best way to avoid people accidentally 
introducing subtle bugs, is to ensure the language is designed to not 
let them do so!).

Warmest Regards,

Mark.

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps




More information about the use-livecode mailing list