lua-users home
lua-l archive

Re: orif

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Soni L. once stated:
> (Copypasted from 
> https://gist.github.com/SoniEx2/99882fa8d5a0740339b0#the-orif-idea )
> 
> The orif Idea
> =============
> 
> orif (possible operators: `|if`, `orif`, `or if`, ...) is a construct that 
> allows if statements with **explicit** fall-through.
 It's an interesting idea, but I'm still unsure if I like the name "orif"
or not. I don't really like it, but I can't come up with a better name.
 But, if it's a switch with a fallthrough case you are missing (and
frankly, I really haven't missed it that much in Lua), what about the
following?
	-- ----------------------------
	-- You need this function
	-- ----------------------------
	function switch(key,case,otherwise)
	 otherwise = otherwise or function() return nil end
	 
	 if not case[key] then
	 return otherwise(case,key)
	 else
	 return case[key](case,key)
	 end
	end
	-- ----------------------------
	-- And now you can do this 
	-- ----------------------------
	flag = false
	i = 2
	switch(i,
	 {
	 function() flag = true end,
	 function(case) case[1]() print(flag) end,
	 },
	 function() print "Unknown!" end
	)
 It's about the same syntatical overhead of a C or Java switch statement,
but it does allow fallthrough (or the effect thereof). And it can be used
in an expression context (take THAT, C!).
 -spc (Did not expect this to be so easy ... )

AltStyle によって変換されたページ (->オリジナル) /