fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Implementation request for the SWITCH statement 27 Jul 2020 16:45 #15335

  • Fab64's Avatar

  • Fab64

  • Topic Author


  • Posts: 61
  • Hi, Robert and dev. Team,

    the SWITCH statement is very powerful, but sometimes not comfortable to use (compared to the DO CASE):

    FUNCTION ActonChoice(nChoice as LONG) AS VOID
    SWITCH nChoice
    CASE 0
    RETURN
    CASE 1
    ChoiceOne()
    CASE 2
    ChoiceTwo()
    CASE 3
    CASE 4
    CASE 5
    ..........
    CASE 100
    ChoiceOverFour()
    CASE 101
    ............
    CASE 1000
    ChoiceOVER()
    END SWITCH

    My request is if it were possible to simplify in this way:

    FUNCTION ActonChoice(nChoice as LONG) AS VOID
    SWITCH nChoice
    CASE 0
    RETURN
    CASE 1
    ChoiceOne()
    CASE 2
    ChoiceTwo()
    CASE 3 TO 100
    ChoiceOverFour()
    CASE 101 TO 1000
    ChoiceOVER()
    END SWITCH

    best regards,
    Fabrizio

    Please Log in or Create an account to join the conversation.

    Implementation request for the SWITCH statement 27 Jul 2020 17:10 #15336

  • Chris's Avatar

  • Chris


  • Posts: 2039
  • Hi Fabrizio,

    Unfortunately this is not supported in any .Net language. In short, the reason is that SWITCH is designed to be very fast, by creating a "jump table", which decides with just one operation, where to make the jump, depending on the value of the SWITCH variable. If you start adding ranges of values, then each of those ranges need to be evaluated (if the value falls inside them), so the jump cannot be done in one operation and the advantage of the SWITCH command is lost.

    Theoretically, the compiler could implicitly translate

    CASE 3 TO 100

    to

    CASE 3
    CASE 4
    CASE 5
    CASE 6
    ....
    CASE 100

    and emit all that in the IL code, but in this case I guess the generation of the jump table will probably create a huge overhead, so again there is no reason to use SWITCH. In such cases, it's just better to simply use DO CASE (or IF...ELSEIF) instead.

    Edit: Although the first answer in this seems to suggest that this functionality has actually been implemented in c# 7.0! Hmm, need to check... stackoverflow.com/questions/20147879/swi...tead-of-a-one-number

    Edit2: Looks like this feature is included in c# 9.0: www.infoq.com/news/2020/07/CSharp-9-Range-Patterns/
    So sooner or later, this can be included in X# as well! So please forget what I said above :) I guess it still makes sense to prefer it, compared to DO CASE even in this case, since it still provides compile-time checking for multiple or incorrect type of values etc.
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Last edit: by Chris.

    Implementation request for the SWITCH statement 29 Jul 2020 23:13 #15369

  • Fab64's Avatar

  • Fab64

  • Topic Author


  • Posts: 61
  • Hi Chris,

    ok I will use Do Case / If until X # is aligned with C # 9.0

    best regard.
    Fabrizio

    Please Log in or Create an account to join the conversation.

    Implementation request for the SWITCH statement 29 Jul 2020 23:50 #15371

  • Chris's Avatar

  • Chris


  • Posts: 2039
  • Hi Fabrizio,

    Well, c# 9.0 is not even released yet :)
    But it should be, in the next few months.
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Last edit: by Chris.
    • Page:
    • 1