Select...Case fallthrough

Discuss the Firewing language

Select...Case fallthrough

Postby majenko » Fri Dec 28, 2012 3:23 pm

Is there any facility for Select...Case "case fallthrough"?

In C I am used to doing such things as:

Code: Select all
switch (var) {
  case 'a':
    call_function_a();
    break;
  case 'b':
    call_function_b();
  case c:
    call_function_c();
    break;
}
... etc.

In that example, if "var" is 'a', then function a is called. If it is 'c', then function c is called. However, if it is 'b', then both functions b and c are called. Obviously, like that it's pretty pointless, but put some sizeable chunks of code in those case blocks, and you have a very powerful tool.

I see nothing in the Select...Case of FW that allows this - the break is implicit in all case blocks whether you want it or not it seems. I guess it could be fudged using a (ugh) goto... :/
majenko
 
Posts: 13
Joined: Thu Nov 08, 2012 8:44 pm

Re: Select...Case fallthrough

Postby David John Barker » Sat Dec 29, 2012 6:55 pm

The short answer is no. Case statements in "C" and Firewing are quite different animals. As you have already observed, the main difference being that "C" has an explicit break statement whereas Firewing has an implicit break. The Firewing syntax and semantics are therefore more more closely related to VB, C#, PASCAL et al. In most cases this isn't a problem. In fact, you can do some pretty cool stuff. For example,
Code: Select all
   Select value * 2
   Case < 10, > 100
      result = 1
   Case 10 To 20, 50 To 100
      result = 2
   Case Else
      result = 0
   End Select

However, as you have already mentioned, falling through to "case C" like in your example is more problematic. It's best to try and solve the problem by design rather than resorting to lot's of "gotos". That said, if you think in "C" or are porting from "C", then you probably want to maintain a similar construct. To some extent, you can work around your example problem using something like:
Code: Select all
   Select (var)
   Case "a"
      call_function_a()
   Case "b", "c"
      call_function_b
      if var = "c" then call_function_c()
   End Select

Notice the single line "if" statement - it's nice and compact for this type of thing. However, I would accept things become a little more cumbersome if the fall through clause has multiple statements.
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

x