Writing Extensions
Mark Waddingham
mark at livecode.com
Wed May 24 06:03:18 EDT 2017
Hi Kay
On 2017-05-24 10:19, Kay C Lan via use-livecode wrote:
> In that last one the only inference I could find was that due to the
> probability that in 97% of use 'fall through' is not intended, a whole
> heap of typing could be saved if no keyword (break) was required for
> the 97%, and a keyword was only used to activate fall through. i.e.
>
> switch yourName
> case "Mark"
> -- do something
> break
> case "John"
> case "Jon"
> -- do somethingElse
> break
> case "Ali"
> -- do anotherThing
> break
> end switch
>
> would become
>
> switch yourName
> case "Mark"
> -- do something
> case "John"
> fall --or some other keyword
> case "Jon"
> -- do somethingElse
> case "Ali"
> -- do anotherThing
> end switch
>
>
> 11 less character to write. Is this the only problem with the current
> implementation of Switch?
Okay so I was perhaps a little vociferous in what I said... However,
fallthrough is
the key problem - there have been numerous subtle bugs in the engine due
to it
(similarly, switch statements without 'default' can cause subtle
bugs too - if you add a value to an enum, but don't update all the
switches which
switch on it).
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).
However, it would probably be better to change the switch syntax to use
different keywords (perhaps choice?). The 'switch'/'case'/'break'
paradigm is so ubiquitous (due to C) that making it work differently
would be very jarring.
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