lua-users home
lua-l archive

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


On Mar 1, 2013, at 19:53 , Miles Bader wrote:

> Sven Olsen <sven2718@gmail.com> writes:
>> That's probably another good reason avoid turning '?' into a unary postfix
>> operator -- doing so would badly confuse Ruby and CoffeeScript people :)
> 
> I agree that limiting this functionality to "?." and "?[" is probably
> better.
> 
> A more general "?" operator might be _possible_, but doesn't actually
> seem terribly _useful_.  [If someone has counterexamples to this --
> cases where a "?" postfix operator that _isn't_ immediately followed
> by "." or "[" is a significant convenience -- please post them!]

I actually had a use-case for a general ? postfix operator - not related to this, though. A while back, I had the idea to alleviate the "storing nil in tables" pain by having nil as a legitimate value in tables, and instead of having t[k] return nil on a nonexistent key, have it return no values. In most cases (i.e. everywhere except the end of a function call), that would end up as nil anyway, so an additional idea I had was a ? postfix operator that indicates whether the expression before it has any return values. So:

> t = {}
> print(t.foo)

> print((t.foo))
nil
> print(t.foo?)
false
> t.foo = nil
> print(t.foo)
nil
> print((t.foo))
nil
> print(t.foo?)
true

I've been meaning to write up a formal proposal for the idea for a while, but haven't found the time.

Thanks,
Matthew Frazier
http://leafstorm.us/