orif

classic Classic list List threaded Threaded
39 messages Options
12
Reply | Threaded
Open this post in threaded view
|

orif

Soni "They/Them" L.
(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)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: orif

Laurent FAILLIE
Hum, GOOD idea :)


Le Vendredi 14 août 2015 19h55, Soni L. <[hidden email]> a écrit :


(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)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.




Reply | Threaded
Open this post in threaded view
|

Re: orif

Coda Highland
In reply to this post by Soni "They/Them" L.
On Fri, Aug 14, 2015 at 10:55 AM, Soni L. <[hidden email]> 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)

Reply | Threaded
Open this post in threaded view
|

Re: orif

Tobias Kieslich
In reply to this post by Soni "They/Them" L.

Quoting "Soni L." <[hidden email]>:

>
> 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
> ```

Lua has elseif which is I think exactly the same:
local flag = false
if i == 1 then
     flag = true
elseif i == 2 then -- explicit fallthrough
     print(flag)
else -- default
     print "Unknown!"
end

Regards,
   Tobias


Reply | Threaded
Open this post in threaded view
|

Re: orif

Coda Highland
On Fri, Aug 14, 2015 at 11:29 AM,  <[hidden email]> wrote:

>
> Quoting "Soni L." <[hidden email]>:
>
>>
>> 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
>> ```
>
>
> Lua has elseif which is I think exactly the same:
> local flag = false
> if i == 1 then
>     flag = true
> elseif i == 2 then -- explicit fallthrough
>     print(flag)
> else -- default
>     print "Unknown!"
> end
>
> Regards,
>   Tobias

No, elseif is totally different semantics, and elseif is exactly WHY I
think orif is a bad idea: elseif only fires if an earlier condition
wasn't true. orif looks syntactically like it ought to behave like
elseif, but it doesn't.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: orif

Jcd1230
Using do ... end can already be used to achieve something very similar to a switch statement, with fallthrough:
To take your example:
local flag = false
do
  if (i == 1) then
    flag = true
  end

end



On Fri, Aug 14, 2015 at 2:32 PM Coda Highland <[hidden email]> wrote:
On Fri, Aug 14, 2015 at 11:29 AM,  <[hidden email]> wrote:
>
> Quoting "Soni L." <[hidden email]>:
>
>>
>> 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
>> ```
>
>
> Lua has elseif which is I think exactly the same:
> local flag = false
> if i == 1 then
>     flag = true
> elseif i == 2 then -- explicit fallthrough
>     print(flag)
> else -- default
>     print "Unknown!"
> end
>
> Regards,
>   Tobias

No, elseif is totally different semantics, and elseif is exactly WHY I
think orif is a bad idea: elseif only fires if an earlier condition
wasn't true. orif looks syntactically like it ought to behave like
elseif, but it doesn't.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: orif

Andrew Starks
In reply to this post by Coda Highland
On Fri, Aug 14, 2015 at 1:31 PM, Coda Highland <[hidden email]> wrote:

> On Fri, Aug 14, 2015 at 11:29 AM,  <[hidden email]> wrote:
>>
>> Quoting "Soni L." <[hidden email]>:
>>
>>>
>>> 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
>>> ```
>>
>>
>> Lua has elseif which is I think exactly the same:
>> local flag = false
>> if i == 1 then
>>     flag = true
>> elseif i == 2 then -- explicit fallthrough
>>     print(flag)
>> else -- default
>>     print "Unknown!"
>> end
>>
>> Regards,
>>   Tobias
>
> No, elseif is totally different semantics, and elseif is exactly WHY I
> think orif is a bad idea: elseif only fires if an earlier condition
> wasn't true. orif looks syntactically like it ought to behave like
> elseif, but it doesn't.
>
> /s/ Adam
>


Points + AND - for being clever, though. This is one of the times when
I *feel* like I'm inefficient in lua:

local the_type = type(foo)

if the_type == 'table' then
  --- something
elseif type == 'number' then
--- blah


---

or some other/better example. I don't know if it *is* slow, however.
I'm not qualified to judge `orif` innovative or otherwise, but given
the volume of Sonic's proposals, I was surprised to find myself
intrigued by this one. FWIW

-Andrew

Reply | Threaded
Open this post in threaded view
|

Re: orif

Duncan Cross
In reply to this post by Soni "They/Them" L.
On Fri, Aug 14, 2015 at 6:55 PM, Soni L. <[hidden email]> wrote:
> The orif Idea
> =============
>
> orif (possible operators: `|if`, `orif`, `or if`, ...) is a construct that
> allows if statements with **explicit** fall-through.

If you want explicit fall-through, I feel like the right way would be
to make it a special case that goto can "see" a jump-label if it comes
immediately after the "else" or "elseif (...) then" that ends the
current block.

(But personally, it's not something that I miss having, when writing Lua code.)

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: orif

Dirk Laurie-2
In reply to this post by Andrew Starks
2015-08-14 20:48 GMT+02:00 Andrew Starks <[hidden email]>:
> Points + AND - for being clever, though. This is one of the times when
> I *feel* like I'm inefficient in lua:
>
> local the_type = type(foo)
>
> if the_type == 'table' then
>   --- something
> elseif type == 'number' then
> --- blah

It's verbose but clear. Lots of ways to obfuscate it. E.g.

({table = function() something end,
  number = function() blah end})[the_type]()

I love Soni's suggestions. They make me appreciate how much
thought the Lua team have put into making the language lean
but powerful, concise but expressive, subtle but logical.

Reply | Threaded
Open this post in threaded view
|

Re: orif

Duncan Cross
In reply to this post by Andrew Starks
On Fri, Aug 14, 2015 at 7:48 PM, Andrew Starks <[hidden email]> wrote:
> Points + AND - for being clever, though. This is one of the times when
> I *feel* like I'm inefficient in lua:
>
> local the_type = type(foo)
>
> if the_type == 'table' then
>   --- something
> elseif type == 'number' then
> --- blah

I don't see how the given proposal would affect this, though? The
proposal only adds the ability for switch-like fall-throughs, not
switch-like case syntax - you'd still have to do full (x == y)
comparisons in each clause.

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: orif

Soni "They/Them" L.
In reply to this post by Coda Highland


On 14/08/15 03:31 PM, Coda Highland wrote:

> On Fri, Aug 14, 2015 at 11:29 AM,  <[hidden email]> wrote:
>> Quoting "Soni L." <[hidden email]>:
>>
>>> 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
>>> ```
>>
>> Lua has elseif which is I think exactly the same:
>> local flag = false
>> if i == 1 then
>>      flag = true
>> elseif i == 2 then -- explicit fallthrough
>>      print(flag)
>> else -- default
>>      print "Unknown!"
>> end
>>
>> Regards,
>>    Tobias
> No, elseif is totally different semantics, and elseif is exactly WHY I
> think orif is a bad idea: elseif only fires if an earlier condition
> wasn't true. orif looks syntactically like it ought to behave like
> elseif, but it doesn't.
>
> /s/ Adam
>
orif is the or operator combined with elseif.

It's similar to this (if Lua allowed this):

local flag = false
if i == 1 then
   flag = true
   goto next
elseif i == 2 then
   ::next::
   print(flag)
else
   print"Unknown!"
end

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: orif

Jonathan Goble
On Fri, Aug 14, 2015 at 3:01 PM, Soni L. <[hidden email]> wrote:

> orif is the or operator combined with elseif.
>
> It's similar to this (if Lua allowed this):
>
> local flag = false
> if i == 1 then
>   flag = true
>   goto next
> elseif i == 2 then
>   ::next::
>   print(flag)
> else
>   print"Unknown!"
> end

This can be written in stock Lua without any new features by using
nested if blocks:

local flag = false
if i == 1 or i == 2 then
  if i == 1 then
    flag = true
  end
  print(flag)
else
  print"Unknown!"
end

Reply | Threaded
Open this post in threaded view
|

Re: orif

Soni "They/Them" L.


On 14/08/15 04:08 PM, Jonathan Goble wrote:

> On Fri, Aug 14, 2015 at 3:01 PM, Soni L. <[hidden email]> wrote:
>> orif is the or operator combined with elseif.
>>
>> It's similar to this (if Lua allowed this):
>>
>> local flag = false
>> if i == 1 then
>>    flag = true
>>    goto next
>> elseif i == 2 then
>>    ::next::
>>    print(flag)
>> else
>>    print"Unknown!"
>> end
> This can be written in stock Lua without any new features by using
> nested if blocks:
>
> local flag = false
> if i == 1 or i == 2 then
>    if i == 1 then
>      flag = true
>    end
>    print(flag)
> else
>    print"Unknown!"
> end
>
But now you check for the condition twice.

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: orif

Jonathan Goble
On Fri, Aug 14, 2015 at 3:17 PM, Soni L. <[hidden email]> wrote:

> On 14/08/15 04:08 PM, Jonathan Goble wrote:
>> This can be written in stock Lua without any new features by using
>> nested if blocks:
>>
>> local flag = false
>> if i == 1 or i == 2 then
>>    if i == 1 then
>>      flag = true
>>    end
>>    print(flag)
>> else
>>    print"Unknown!"
>> end
>>
> But now you check for the condition twice.

I never said it was a good way to write it. I just said it was possible.

Reply | Threaded
Open this post in threaded view
|

Re: orif

Laurent FAILLIE
In reply to this post by Soni "They/Them" L.
ORIF should be a great idea to make code easier to maintain without switch/case construct, IMOO.
 



Le Vendredi 14 août 2015 21h02, Soni L. <[hidden email]> a écrit :




On 14/08/15 03:31 PM, Coda Highland wrote:

> On Fri, Aug 14, 2015 at 11:29 AM,  <[hidden email]> wrote:
>> Quoting "Soni L." <[hidden email]>:
>>
>>> 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
>>> ```
>>
>> Lua has elseif which is I think exactly the same:
>> local flag = false
>> if i == 1 then
>>      flag = true
>> elseif i == 2 then -- explicit fallthrough
>>      print(flag)
>> else -- default
>>      print "Unknown!"
>> end
>>
>> Regards,
>>    Tobias
> No, elseif is totally different semantics, and elseif is exactly WHY I
> think orif is a bad idea: elseif only fires if an earlier condition
> wasn't true. orif looks syntactically like it ought to behave like
> elseif, but it doesn't.
>
> /s/ Adam
>
orif is the or operator combined with elseif.

It's similar to this (if Lua allowed this):

local flag = false
if i == 1 then
  flag = true
  goto next
elseif i == 2 then
  ::next::

  print(flag)
else
  print"Unknown!"
end


--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.





Reply | Threaded
Open this post in threaded view
|

Re: orif

Rena
In reply to this post by Andrew Starks
On Fri, Aug 14, 2015 at 2:48 PM, Andrew Starks <[hidden email]> wrote:

> On Fri, Aug 14, 2015 at 1:31 PM, Coda Highland <[hidden email]> wrote:
>> On Fri, Aug 14, 2015 at 11:29 AM,  <[hidden email]> wrote:
>>>
>>> Quoting "Soni L." <[hidden email]>:
>>>
>>>>
>>>> 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
>>>> ```
>>>
>>>
>>> Lua has elseif which is I think exactly the same:
>>> local flag = false
>>> if i == 1 then
>>>     flag = true
>>> elseif i == 2 then -- explicit fallthrough
>>>     print(flag)
>>> else -- default
>>>     print "Unknown!"
>>> end
>>>
>>> Regards,
>>>   Tobias
>>
>> No, elseif is totally different semantics, and elseif is exactly WHY I
>> think orif is a bad idea: elseif only fires if an earlier condition
>> wasn't true. orif looks syntactically like it ought to behave like
>> elseif, but it doesn't.
>>
>> /s/ Adam
>>
>
>
> Points + AND - for being clever, though. This is one of the times when
> I *feel* like I'm inefficient in lua:
>
> local the_type = type(foo)
>
> if the_type == 'table' then
>   --- something
> elseif type == 'number' then
> --- blah
>
>
> ---
>
> or some other/better example. I don't know if it *is* slow, however.
> I'm not qualified to judge `orif` innovative or otherwise, but given
> the volume of Sonic's proposals, I was surprised to find myself
> intrigued by this one. FWIW
>
> -Andrew
>

This is a good example of why DRY is a good idea: you avoid mistakes
like comparing to the wrong variable in your elseif there. :-)

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: orif

Patrick Donnelly
In reply to this post by Soni "They/Them" L.
On Fri, Aug 14, 2015 at 1:55 PM, Soni L. <[hidden email]> wrote:

> 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
> ```

Without commenting on the idea itself, I think "thenif" makes more
sense than "orif".

--
Patrick Donnelly

Reply | Threaded
Open this post in threaded view
|

Re: orif

Andrew Starks
In reply to this post by Rena
On Fri, Aug 14, 2015 at 2:59 PM, Rena <[hidden email]> wrote:
> This is a good example of why DRY is a good idea: you avoid mistakes
> like comparing to the wrong variable in your elseif there. :-)

Ha! Yes. Soni had the right example in his OP, anyway. Even, actually
especially, mistakes can reveal something.

I more wanted to say that I thought the suggestion had intrigue,
mostly for being unique to me and somewhat clever. I've found that
it's hard but rewarding to acknowledge merit in those we don't agree
almost always disagree with Soni. ;)

In fun,

-Andrew

Reply | Threaded
Open this post in threaded view
|

Re: orif

Soni "They/Them" L.
In reply to this post by Patrick Donnelly


On 14/08/15 05:02 PM, Patrick Donnelly wrote:

> On Fri, Aug 14, 2015 at 1:55 PM, Soni L. <[hidden email]> wrote:
>> 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
>> ```
> Without commenting on the idea itself, I think "thenif" makes more
> sense than "orif".
>
"thenif" makes it seem like an if inside an if.

"orif" is literally the "or" operator (can't stress this enough)
combined with "if".

It's similar to doing

local flag = false
local function f()
   flag = true
end
if (i==1 and (f() or true)) or i==2 then -- (f() or true) is needed
because we always want it to be true)
   print(flag)
else
   print("Unknown!")
end

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: orif

Duncan Cross
On Fri, Aug 14, 2015 at 9:56 PM, Soni L. <[hidden email]> wrote:
> "orif" is literally the "or" operator (can't stress this enough) combined
> with "if".

It really doesn't work that well as an explanation, I'm afraid. I know
Lua pretty well, and it would not be clearly intuitive to me that "or
operator combined with if" means what you describe in the proposal.

-Duncan

12