HAI
CAN HAS STDIM?
TXT ME A COLOR

WTF IZ COLOR? 
OMG RED
 	VISIBLE "U SED RED"
	0WND
OMG BLUE
	VISIBLE "U SED BLUE"	
 	0WND
WTF!
 VISIBLE "COLOR NOT FOUND"
 (OWND) :) VISIBLE "YOU LOZE!" :< VISIBLE "YOU WINS"
KTHXBYE

Ooh, nice. I like it overall. Are you suggesting 0WND as a keyword or a location to break to? Also, the final line is a little heavy on punctuation, and too terse as a result. What's happening there? Can it be expressed in words? — Adam, 2007/05/28 20:41

I suggest OMGWTF! as the fallback case. Or alternatively, as the closing of the WTF block. If there is no special closing, then shouldn't the example have two KTHXBYE lines? I'm not sure what that last line is doing either…it could be a tertiary operator… — ILikePi, 2007/05/29 03:06


I don't really see WTF being proper in this case, as I see it having more drastic consequences than a simple switch… I also propose the following modifications (equivalent to the code above except with the fallback case):

HAI
CAN HAS STDIN?
TXT ME A COLOR

WATZ COLOR? 
  OMG RED
    VISIBLE "U SED RED"
    SEE
  OMG BLUE
    VISIBLE "U SED BLUE"	
    SEE
  OMG
    VISIBLE "I DUNNO WAT U SED"
    SEE
OIC
KTHXBYE

I see, however, one problem with the switch-case construct: they are implemented in bytecode with a jump/goto/lookup thingy (you assembly people understand) that really only makes sense for compiled languages and integral values for the switch… hence their slight performance improvement over if/else. This doesn't really apply in an interpreted language, though it is still a nice syntax for a long series of elseifs, so it really isn't a problem so much as a question of motivation. I think we should hold off on actually implementing this one, as in the interpreted case (and in the case where it is actually allowing non-integral switch values) it degrades to ifs, and so is not necessary for the first versions of LOLCODE.

– Stylistically speaking, I'd have it this way:

HAI
 CAN HAS STDIM?
 TXT ME A COLOR
 WTF IZ COLOR? 
   OMG RED
 	VISIBLE "U SED RED"
	0WND
   OMG BLUE
	VISIBLE "U SED BLUE"	
 	0WND
   WTF!
        VISIBLE "COLOR NOT FOUND"
        BYE 1
        ^^ Halt with a failed error code.
 KTHX
KTHXBYE

I would certainly keep the “WTF!”, it is 100% LOLCODE. It has no real meaning other than to remind the programmer that this is an unexpected code flow section. The ”(OWND) :) VISIBLE “YOU LOZE!” :< VISIBLE “YOU WINS”” of course had to go, A) it was redundent, the user already has an error message for the all outcomes of the switch statement. B) It seemed indecipherable at first glance – not what LOLCODE is about. The KTHX instead of the KTHXBYE I think is important, as this allows us to reserve the term “BYE” with halting the programme. KTHXBYE must always be at the end of a program, just as HAI must be at the beginning. KTHXBYE returns a 0 (success) return value for the process. KTHX I think could be used generally as the end of functions or whatever.

^^ Having commments like this line with the two arrows
^^ I think is appropriate as this is how I would comment
^^ on another line in IRC, and it is pretty clear what
^^ line it is commenting on, and it is very neat,
^^ compared to say # this hash type of comment which
^^ trails of the screen.. and which everyone uses anyway
^^ so LOLCODE wouldn't use that.

And finally, I think STDIM should be (one of many) officially accepted alias('s) for STDIO. Just so that we don't slow our programmers down with unneccassary typos.

– skiy


Possibly WTFBBQ for the default case?

Church Tucker, 2007/05/30 17:08


I really like this general construction, but I don't know if 0WND suggests the correct completion for WTF IZ var? OMG value.

In fact, do we really need a 'break' statement here at all? Accidental switch-case fall-through is a very common programming error that we could eliminate completely right now. Why don't we simplify it:

HAI
 CAN HAS STDIO?
 GIMMEH COLOR
 WTF IZ COLOR? 
   OMG "RED"
     VISIBLE "U SED RED"
   OMG "BLUE"
     VISIBLE "U SED BLUE"	
   OMG "YELLOW" "ORANGE" "WHITE"
     VISIBLE "TOO BRIGHT 4 ME!" 
   WTF!
        VISIBLE "COLOR NOT FOUND"
 KTHX
KTHXBYE 

Nate Austin, 2007/05/31 00:59


To ammend my earlier suggestion, what if OMGWTF were the default? Then the above would read:

HAI
 CAN HAS STDIO?
 GIMMEH COLOR
 WTF IZ COLOR? 
   OMG "RED"
     VISIBLE "U SED RED"
   OMG "BLUE"
     VISIBLE "U SED BLUE"	
   OMG "YELLOW" "ORANGE" "WHITE"
     VISIBLE "TOO BRIGHT 4 ME!" 
   OMGWTF?
        VISIBLE "COLOR NOT FOUND"
 KTHX
KTHXBYE 

Church Tucker, 2007/05/31 05:37


How about OSHI or IONO?

— Al, 2007/05/31


This is probably the funniest code on the entire site, so we should definetly keep it! I like the suggestions that OMGWTF is default, and that we get rid of the breaks. I don't think I have ever used a switch/case fall through for anything useful, and having break/PWND just increases the amount of keys pressed.

This should definitely be in v1.5! — Marius, 2007/05/31 11:36


I agree with Church Tucker's example. I like it, has a LOLness to it and actually makes more sense than switch/case does IMO.

Cerberus, 2007/06/01 08:08

contributions/switch-case.txt · Last modified: 2007/06/01 08:09 by cerberus
Recent changes RSS feed Creative Commons License Donate Driven by DokuWiki