Adding switch(){} statement to jasshelper (good/bad idea)?

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
I had the idea, while doing c++, to suggest adding the switch statement to jasshelper. (For those who don't know what the switch statement is: Switch statement)

Exemple
JASS:

function Test takes nothing returns nothing
    local integer value=15
    switch(value)
        case 5 {
            call DoSomething(5)
            break
        }
        case 10 {
            call DoSomething(10)
            break
        }
        case 15 {
            call DoSomething(15)
            break
        }
        default {
            call DoNothing()
            break
        }
    endswitch
endfunction


Good idea? Opinions please :).
 

jomik

New Member
Reaction score
17
Well, your example looks like it could be rewritten to:
JASS:
function Test takes nothing returns nothing
    local integer value=15
    if (value != 0)
        call DoSomething(value)
    endif
endfunction


But if it is what I think it is, then it's goodies :D
But it can already be made with if statements?
 

Tyrulan

Ultra Cool Member
Reaction score
37
If statements yes, but this is more organized. I like it. Plus shorter to write if you have many conditions you avoid long statements ... or or or and or or and and or....
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Basicly, it would be rewriten with if statements but the main idea is that it's shorter to write and looks neater, Imo..
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Suggest to cJass, it uses c style. vJass is fine, if you want to use shortcut, use Zinc.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Yeah.
I'd remove the endswitch keyword though.
 

Jesus4Lyf

Good Idea™
Reaction score
397
It is all crap. It is more efficient (and perhaps even readable) if you have enough cases to do:
JASS:
function Test takes nothing returns nothing
    local integer value=15
    call MySwitchCases[value].evaluate()
endfunction

And fill "MySwitchCases" with function pointers on init. :)

Heck, you can make MySwitchCases a trigger array directly and add triggers with conditions and just do [LJASS]call TriggerEvaluate(MySwitchCases[value])[/LJASS]. :thup:

(Switch is just an O(n) complexity way of doing this, so to speak.)
 

Nestharus

o-o
Reaction score
84
In JASS, it really wouldn't matter whether you were using a switch statement or a set of ifs. In compiled programming, the switch statement immediately goes to the set case. In compiled programming, switch statements are faster than ifs -.-, but this isn't compiled programming is it? It's interpretive, so it really doesn't matter which way or the other ;).


Jesus4Lyf's a good idea for maintainability and for speed if you end up having a million else ifs.

I don't believe a switch statement would merit any benefit whatsoever to the JASS language ;D.
 

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
This would not be really possible in Jass, because if 1 case returns true it should execute every case below as well.
Meaning this can only be done with a lot of preset booleans for a case.

So your example should be converted to this:
JASS:
function Test takes nothing returns nothing
    local integer value=15
    local boolean a = false
    local boolean b = false
    local boolean break = false

    if( value == 5 ) then
        set a = true
        set break = true
        call DoSomething(value)
    elseif (value == 10) or ((break == false) and (a == true)) then
        set b = true
        set break = true
        call DoSomething(value)
    elseif (value == 15) or ((break == false) and (b == true)) then
        call DoSomething(value)
    elseif (true) and ( (break == false) and (a == false) and (b == false)) then // dunno if default switch case is executed when every case returns false, otherwise it's a simple "elseif true then")
        call DoNothing()
        // The last break here in your code does not make any sense.
    endif
endfunction


Perhaps this code is not fully accurate in the way switch really works but you should get the idea. This causes a lot of technical difficulties and a lot of more compute time as well.

If-then-else works good, if you don't like the syntax use zinc, otherwise don't complain.
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
I really don't see what the point of this is.
 

Nestharus

o-o
Reaction score
84
You and me both

In JASS, it really wouldn't matter whether you were using a switch statement or a set of ifs. In compiled programming, the switch statement immediately goes to the set case. In compiled programming, switch statements are faster than ifs -.-, but this isn't compiled programming is it? It's interpretive, so it really doesn't matter which way or the other .


Jesus4Lyf's a good idea for maintainability and for speed if you end up having a million else ifs.

I don't believe a switch statement would merit any benefit whatsoever to the JASS language ;D.
 

Nestharus

o-o
Reaction score
84
Switch statement is just multi branch goto...


jesus4lyf, given who you are I didn't expect I'd have to explain that to you ...


Again, switch statements in JASS are impossible... if you are doing it just for syntax that's just totally misleading, not to mention pointless.


Now, you may call my statements wrong when it comes to JASS, but when it comes to the real world, you'll find I'm right. JASS doesn't follow real world trends, so I may use real world examples since I assume JASS does the same thing.

I've learned most of what you say is assumptions

And where on earth do I get those assumptions? Now we are talking about a feature from real world programming languages, not from JASS, so this is not at all an assumption. If I ever have an assumption, it only ever has to do with JASS relating to hard cold facts from real world languages. I don't just pull them out of a hat.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
lol ninja edit

i see wut u did thar


I think switch is a really good idea, who cares about efficiency? People who want to be efficient will know to avoid switch.
 

Nestharus

o-o
Reaction score
84
Narks, that post made 0 sense unless you are thinking of using pure trigger evaluations ;|.


Trigger evaluations would be similar to a switch statement actually, but... they'd be hella slow ^^

And if you wanted to do labels.. that'd be like multiple trigger evaluations per trigger evaluation o-o.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
Actually now that I've read up switch statements, I realize they are different from the "Select Case" statements in visual failbasic.

I thought switch was just like a bunch of if / else statements.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top