Syntax Addition

lua-users home
wiki

By making a small change to the function llex and the array luaX_tokens in "llex.c" we can achieve the same syntax as C, JavaScript and other languages for the "not" and "not equal" operators.

After the changes bellow in the Lua source we can write:

if !b then a = c end

if a != c then a = c end

and it still works with the originals

if not b then a = c end

if a ~= c then a = c end

Comments

I agree with !=, especially as ~= is not widely used elsewhere and could be confused with Perl's =~. However, ! for not is bad, as it goes against Lua's rule of using words rather than symbols (compare with and and or). -- ReubenThomas

~ is a valid mathematical symbol for logical negation [1]. In programming languages, it is less common. However, even in languages like C, ~ is used for the closely related bitwise negation, while ! is used for logical negation, and != is used for inequality (i.e. !(a==b)). So, Lua's ~= has a small resemblance. However, C is inconsistent between bitwise and logical operations: & and &&, | and ||, and ~ and ! -- why not ~~ for logical negation to avoid a different character? In Lua, you might alternately do not(a==b). -- DavidManura

Or perhaps instead use <> as per Basic... thus requiring only the three comparison characters <, >, = for all tests. The ~ character is currently unique in Lua as being the only one that starts a multi-character lexical item while not being one itself! Also, <> matches the <= usage, so that x<=y means x<y or x=y while x<>y means x<y or x>y. Consistent. -- PeterHill

IMO, <> for inequality is a bad idea. The are data for which < and > may make no sense at all. For points in space, e.g., you may define equality and inequality, but there is no linear ordering. Yet you may still want to have a test for (in)equality of points, and <> might be misleading. -- Hgr

It is only consistent if you have complete [2] ordering. Otherwise it is possible for all three of <, > and == to be false. Even though ~ is annoyingly difficult to type on a Spanish Windows keyboard, which I am currently using to write this message, I have gotten used to it -- and I certainly wouldn't want to have to change all of the code I've got kicking around. Someday we will all have Unicode and be able to use &U+2260; which refuses to render itself on this Wiki. -- RiciLake

Using ! as negation would certainly make picking up Lua somewhat less annoying for programmers of most other languages. Which other languages even use ~ as negation? It's just odd, especially when ~ is used in other languages for things other than negation. Nobody said anything about replacing ~ as negation, simply adding ! as an alternative. So all your existing code continues to work perfectly and doesn't have to be touched at all. --DanHollis?

For what it's worth: Smalltalk, Dylan, and Matlab all use ~= for some kind of inequality according to the [Syntax Across Languages] page. (They also use ~ as the prefix for logical not.) As I understand, it's fairly common to write logical negation (in the mathematical sense) with a ~ prefix, so the use of ~= to mean not equivalent seems like a perfectly natural extension. --TaylorVenable

Supporting both symbols, as suggested above, complicates an otherwise simple language. -- DavidManura

On some keyboards, the ~ is difficult to find and to use. For instance, on azerty, you have to do [alt gr]+[3]+[anykey] to type one. -- anonymous

Wiki Note: The name of this page (SyntaxAddition) is not very descriptive. Perhaps NegationSyntax? would be better.


RecentChanges · preferences
edit · history
Last edited May 2, 2009 1:43 am GMT (diff)