Overloaded logic operators

Mike Austin <mike@mike-austin.com>
Sun, 23 Nov 2008 13:13:28 -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)
[11 later articles]
| List of all articles for this month |
From: Mike Austin <mike@mike-austin.com>
Newsgroups: comp.compilers
Date: Sun, 23 Nov 2008 13:13:28 -0800
Organization: at&t http://my.att.net/
Keywords: design, question
Posted-Date: 23 Nov 2008 18:41:12 EST

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


I'm torn between this being easier for the programmer, or just bad
practice in general.


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"


In languages such as Ruby, Lua, Io, Python and JavaScript, does the
benefit outweigh the effect of overloaded logic?


Mike


Post a followup to this message

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