lua-users home
lua-l archive

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


On Fri, Aug 14, 2015 at 10:55 AM, Soni L. <fakedme@gmail.com> wrote:
> (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.
>
> Trivial use-case
> ----------------
>
> Take the following piece of Java:
>
> ```java
> boolean flag = false;
> switch (i) {
>     case 1:
>         flag = true;
>         // fallthrough
>     case 2:
>         System.out.println(flag)
>         break;
>     default:
>         System.out.println("Unknown!")
> }
> ```
>
> In Lua (there are many ways to do it, I just picked one at random):
>
> ```lua
> local flag = false
> if i == 1 then
>     flag = true
> end
> if i == 1 or i == 2 then
>     print(flag)
> end
> if i ~= 1 and i ~= 2 then
>     print "Unknown!"
> end
> ```
>
> With orif:
>
> ```lua
> local flag = false
> if i == 1 then
>     flag = true
> orif i == 2 then -- explicit fallthrough
>     print(flag)
> else -- default
>     print "Unknown!"
> end
> ```
>
> Differences between orif and switch
> -----------------------------------
>
> Here's a list of pros and cons, where we compare speed, flexibility and
> clarity of switch and orif.
>
> Switch:
>
> - +Essentially jump tables. (speed)
> - -Constant cases. (flexibility)
> - -Fallthrough-by-default (on most programming languages). (clarity)
>
> Orif:
>
> - -If-else chain. (speed)
> - +Lets you test any conditions. (flexibility)
> - +Explicit fallthrough. (clarity)
- -Not common syntax, breaks up conditions for entry into block (clarity)