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) |
[10 later articles] |
From: | "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> |
Newsgroups: | comp.compilers |
Date: | Mon, 24 Nov 2008 09:59:26 +0100 |
Organization: | cbb software GmbH |
References: | 08-11-110 |
Keywords: | design, comment |
Posted-Date: | 24 Nov 2008 18:00:15 EST |
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?
> It enables you do to tricks like this:
>
> x = -1 < x < 1 or 0
>
> but it also means your booleans are values, and you can't test "if x == true",
> but only "if x != nil"
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.
As for macro expressions -1 < x < 1 or 0, which I suppose should read:
(-1 < x) and (idem < 1) or (idem < 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.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
[The A < B < C syntax has been part of Cobol for many decades. It seems
to work fine there. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.