lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Am 08.09.2013 17:46, schrieb steve donovan:
On Sun, Sep 8, 2013 at 4:47 PM, Seppl Vorderhörer
<> wrote:
99% of all programmers seen as) "operations that use conditions and result
in other conditions" are here really misused to not return booleans but
other types.
Using 'or' like this is a popular Lua idiom, often used for default values:

function foo(a,b)
    a = a or 'one'
    b = b or 'two'

It is a _little_ odd at first, but then very liberating.

steve d.

Fully agreed about the "a or 'one' ". This is easy to memorize ("use a or use the next term if a is 'not good' "), perfect for the task of applying defaults and even intuitive after you got rid of the first "odd-ness". ;-)

About the "A and B or C" ... well ... it's very hard for me to see why this is better than "A ? B : C" or maybe a syntax using keywords easier to memorize like e.g. "On (A) Use (B) Otherwise (C)" or whatever which is dedicated to the task to be done (select B or C depending on A).

The confusing thing is that "and" is used in a way that is simply not intuitive but quite the opposite. While "or" is a selection that can easily be viewed as "use A or B" (which fits what is actually happening in Lua) the word "and" has a different meaning in life: "A and B" simply means "use both A and B". "Hey, lets have a beer and a glass of wine" does not mean "wine if the beer is not empty otherwise nothing", no it simply means "lets get drunk by having both". This is what makes "A and B or C" so counterintuitive and the construct hard to read. Although I know that "and" has a completely different meaning in Lua (or any programming language) than in real life and even as I used this construct myself quite a number of times as there's no better substitute it still gives me the creeps when I look at it. It's a construct where I have the feeling, that the oddness will never go away.