[Proposal] Overload | for booleans

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

[Proposal] Overload | for booleans

Soni "They/Them" L.
Can we get true | false to be a thing?

--
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: [Proposal] Overload | for booleans

Dirk Laurie-2
2015-07-29 15:56 GMT+02:00 Soni L. <[hidden email]>:

> Can we get true | false to be a thing?

debug.setmetatable(true,{
  __bor = function(x,y)
    assert (type(x)=='boolean' and type(y)=='boolean')
    return x or y
  end;
    __band = function(x,y)
    assert (type(x)=='boolean' and type(y)=='boolean')
    return x and y
  end;
})

I like that! But why do you call it a proposal?

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Overload | for booleans

Soni "They/Them" L.


On 29/07/15 11:18 AM, Dirk Laurie wrote:

> 2015-07-29 15:56 GMT+02:00 Soni L. <[hidden email]>:
>
>> Can we get true | false to be a thing?
> debug.setmetatable(true,{
>    __bor = function(x,y)
>      assert (type(x)=='boolean' and type(y)=='boolean')
>      return x or y
>    end;
>      __band = function(x,y)
>      assert (type(x)=='boolean' and type(y)=='boolean')
>      return x and y
>    end;
> })
>
> I like that! But why do you call it a proposal?
>
It would be non-short-circuiting and/or.

--
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: [Proposal] Overload | for booleans

Dirk Laurie-2
2015-07-29 16:53 GMT+02:00 Soni L. <[hidden email]>:
>
>
> On 29/07/15 11:18 AM, Dirk Laurie wrote:
>
>> I like that! But why do you call it a proposal?
>>
> It would be non-short-circuiting and/or.

Yes, but as I demonstrated, it is already available for those
who want it. It does not require any change to the language.

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Overload | for booleans

Sean Conner
In reply to this post by Soni "They/Them" L.
It was thus said that the Great Soni L. once stated:

>
>
> On 29/07/15 11:18 AM, Dirk Laurie wrote:
> >2015-07-29 15:56 GMT+02:00 Soni L. <[hidden email]>:
> >
> >>Can we get true | false to be a thing?
> >debug.setmetatable(true,{
> >   __bor = function(x,y)
> >     assert (type(x)=='boolean' and type(y)=='boolean')
> >     return x or y
> >   end;
> >     __band = function(x,y)
> >     assert (type(x)=='boolean' and type(y)=='boolean')
> >     return x and y
> >   end;
> >})
> >
> >I like that! But why do you call it a proposal?
> >
> It would be non-short-circuiting and/or.

  We already have those in the langauge, "and" and "or".  The operators "&",
"|" and "~" work bitwise, not logic wise.  There's a reason why C has both
"|" and "||" for "or".

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Overload | for booleans

Patrick Donnelly
In reply to this post by Dirk Laurie-2
On Wed, Jul 29, 2015 at 11:36 AM, Dirk Laurie <[hidden email]> wrote:

> 2015-07-29 16:53 GMT+02:00 Soni L. <[hidden email]>:
>>
>>
>> On 29/07/15 11:18 AM, Dirk Laurie wrote:
>>
>>> I like that! But why do you call it a proposal?
>>>
>> It would be non-short-circuiting and/or.
>
> Yes, but as I demonstrated, it is already available for those
> who want it. It does not require any change to the language.

I think Soni is suggesting the metatable could be set by the base library.

--
Patrick Donnelly

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Overload | for booleans

Soni "They/Them" L.
In reply to this post by Sean Conner


On 29/07/15 01:11 PM, Sean Conner wrote:

> It was thus said that the Great Soni L. once stated:
>>
>> On 29/07/15 11:18 AM, Dirk Laurie wrote:
>>> 2015-07-29 15:56 GMT+02:00 Soni L. <[hidden email]>:
>>>
>>>> Can we get true | false to be a thing?
>>> debug.setmetatable(true,{
>>>    __bor = function(x,y)
>>>      assert (type(x)=='boolean' and type(y)=='boolean')
>>>      return x or y
>>>    end;
>>>      __band = function(x,y)
>>>      assert (type(x)=='boolean' and type(y)=='boolean')
>>>      return x and y
>>>    end;
>>> })
>>>
>>> I like that! But why do you call it a proposal?
>>>
>> It would be non-short-circuiting and/or.
>    We already have those in the langauge, "and" and "or".  The operators "&",
> "|" and "~" work bitwise, not logic wise.  There's a reason why C has both
> "|" and "||" for "or".
>
>    -spc
>
>
"and" and "or" are the short-circuiting ones. they also do branching.

lua code
-----------------------
function t() print("true") return true end
function f() print("false") return false end

print(t() or f()) -- prints "true" and "true"

print(f() and t()) -- prints "false" and "false"

local left, right = t(), f() -- evaluate both sides, prints "true" and
"false"
print(left or right) -- prints "true", after some branching

local left, right = f(), t() -- evaluate both sides, "false" and "true" now
print(left and right) -- prints "false", after some branching

C code
-------------------------
int t() {
   printf("true\n");
   return 1;
}
int f() {
   printf("false\n");
   return 0;
}
int main() {
   printf("%d", t() || f());
   printf("%d", f() && t());

   printf("%d", t() | f());
   printf("%d", f() & t());

   return 0;
}


--
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: [Proposal] Overload | for booleans

Tim Hill
In reply to this post by Soni "They/Them" L.

> On Jul 29, 2015, at 7:53 AM, Soni L. <[hidden email]> wrote:
>
>
>
> On 29/07/15 11:18 AM, Dirk Laurie wrote:
>> 2015-07-29 15:56 GMT+02:00 Soni L. <[hidden email]>:
>>
>>> Can we get true | false to be a thing?
>> debug.setmetatable(true,{
>>   __bor = function(x,y)
>>     assert (type(x)=='boolean' and type(y)=='boolean')
>>     return x or y
>>   end;
>>     __band = function(x,y)
>>     assert (type(x)=='boolean' and type(y)=='boolean')
>>     return x and y
>>   end;
>> })
>>
>> I like that! But why do you call it a proposal?
>>
> It would be non-short-circuiting and/or.
>
> --
> 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.
>
>

function andx(c1, c2) return c1 and c2 end
function orx(c1, c2) return c1 or c2 end

...
if andx(a < 100, b > 300) then … end

etc. And of course easy to extend andx() and orx() to take varargs for as many expressions as you want.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Overload | for booleans

Soni "They/Them" L.


On 29/07/15 03:05 PM, Tim Hill wrote:

>> On Jul 29, 2015, at 7:53 AM, Soni L. <[hidden email]> wrote:
>>
>>
>>
>> On 29/07/15 11:18 AM, Dirk Laurie wrote:
>>> 2015-07-29 15:56 GMT+02:00 Soni L. <[hidden email]>:
>>>
>>>> Can we get true | false to be a thing?
>>> debug.setmetatable(true,{
>>>    __bor = function(x,y)
>>>      assert (type(x)=='boolean' and type(y)=='boolean')
>>>      return x or y
>>>    end;
>>>      __band = function(x,y)
>>>      assert (type(x)=='boolean' and type(y)=='boolean')
>>>      return x and y
>>>    end;
>>> })
>>>
>>> I like that! But why do you call it a proposal?
>>>
>> It would be non-short-circuiting and/or.
>>
>> --
>> 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.
>>
>>
> function andx(c1, c2) return c1 and c2 end
> function orx(c1, c2) return c1 or c2 end
>
> ...
> if andx(a < 100, b > 300) then … end
>
> etc. And of course easy to extend andx() and orx() to take varargs for as many expressions as you want.
>
> —Tim
>
>
Still does branching (twice, even, because of the function call). The
only difference is that now you evaluate both sides.

--
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: [Proposal] Overload | for booleans

Tim Hill

> On Jul 29, 2015, at 11:09 AM, Soni L. <[hidden email]> wrote:
>
>> function andx(c1, c2) return c1 and c2 end
>> function orx(c1, c2) return c1 or c2 end
>>
>> ...
>> if andx(a < 100, b > 300) then … end
>>
>> etc. And of course easy to extend andx() and orx() to take varargs for as many expressions as you want.
>>
>> —Tim
>>
>>
> Still does branching (twice, even, because of the function call). The only difference is that now you evaluate both sides.
>

Your OP made no mention of branching requirements. And “the only difference” was precisely what you wanted in the OP.

—Tim