Re: Overloaded logic operators

Mike Austin <mike@mike-austin.com>
Tue, 25 Nov 2008 00:39:31 -0800

          From comp.compilers

Related articles
Overloaded logic operators mike@mike-austin.com (Mike Austin) (2008-11-23)
Re: Overloaded logic operators mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2008-11-24)
Re: Overloaded logic operators torbenm@pc-003.diku.dk (2008-11-24)
Re: Overloaded logic operators bc@freeuk.com (Bartc) (2008-11-24)
Re: Overloaded logic operators arnold@skeeve.com (2008-11-25)
Re: Overloaded logic operators mike@mike-austin.com (Mike Austin) (2008-11-25)
Re: Overloaded logic operators mike@mike-austin.com (Mike Austin) (2008-11-25)
Re: Overloaded logic operators mike@mike-austin.com (Mike Austin) (2008-11-25)
Re: Overloaded logic operators lkrupp@pssw.com (Louis Krupp) (2008-11-25)
Re: Overloaded logic operators mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2008-11-25)
Re: Overloaded logic operators m.helvensteijn@gmail.com (2008-11-25)
Re: Overloaded logic operators bc@freeuk.com (Bartc) (2008-11-25)
[6 later articles]
| List of all articles for this month |

From: Mike Austin <mike@mike-austin.com>
Newsgroups: comp.compilers
Date: Tue, 25 Nov 2008 00:39:31 -0800
Organization: at&t http://my.att.net/
References: 08-11-110 08-11-114
Keywords: design
Posted-Date: 25 Nov 2008 07:06:07 EST
X-StarScan-Version: 5.5.12.14.2; banners=-,-,-

Dmitry A. Kazakov wrote:
> On Sun, 23 Nov 2008 13:13:28 -0800, Mike Austin wrote:
>
>> In many languages, logic operators "and" and "or" have been overloaded to
>> handle more than booleans. For example:
>>
>> x = x or 0 # if x is nil, 0
>> x and x.foo() # if x is not nil, call foo
>> x = x > 0 # if x is > 0, x, else false
>
> Is "=" an assignment or equality here? What are the association priorities
> of the operations "=", ">" and "or"? What are the types involved? Do you
> mean short circuit operations, which do not evaluate their arguments
> eagerly?


= as assignment, and I'm specifically interested in null/non-null values for x.
    I should rephrase my first line as "return values based on the operators,
rather than returning true or false". Yes, I do mean short circuit operations.


> Can you explain this? How Booleans cannot be values? To me the operation
> "!=" must yield a result. This result has a value, that value has a type,
> Boolean, I suggest.


Again, I should have said "the results of a comparison operator can
return more than just boolean values".


> As for macro expressions -1 < x < 1 or 0, which I suppose should read:
>
> (-1 < x) and (idem < 1) or (idem < 0)


If a comparison operator returned (this makes them asymmetrical, as Torben has
pointed out). It could return the higher number, or the non-literal, or ...:


((-1 < x) and (x < 1)) or 0


or put another way:


(-1 < x && x < 1) ? x : 0


> here idem denotes the value of the expression x (evaluated strictly once),
> you would need a lot of work to make such macros working. Some time ago I
> played with the idea for a while, but came to the conclusion that it would
> not be worth the efforts and could be very confusing.


Yes, confusion is bad, I think I'll stop work on the idea. :) Thanks for your
insight.


Mike


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.