lua-users home
lua-l archive

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




On Mon, Oct 28, 2019 at 12:57 PM Fernando Jefferson <fjefferson@inf.puc-rio.br> wrote:
In Lua 5.3 interpreter the _expression_ "1 + 1 <enter>" works perfectly.

I even use it at the beginning of my course for beginners, "Programming
for 21st Century", at PUC-Rio, instead of the usual "print ("Hello
World")

"1 + 1" is the simplest computer program that exists.

"Print (" Hello World "), on the other hand, requires a novice
programming student to understand what a function call is, how to pass a
parameter, and know that a quoted phrase is a string.

If you want to teach Lua to someone who knows "C" or "C ++", great.

Now you try to teach programming to a lawyer, a student of literature or
medicine, as we do ...


This is misleading, I'm afraid. It works in the REPL not because it's a valid Lua statement, but because the REPL detects when you've entered an _expression_ instead of a statement and shows the return value. It's as if you wrapped the _expression_ in a print() call.

Now, as far as the original question is concerned... It's been answered THAT expressions aren't statements, but it hasn't been explained WHY. And the answer to why is simply a design decision on Lua's part. Some languages such as C have _expression_ statements, where the return value is discarded. Lua doesn't do this, and the rationale is probably similar to why assignments are statements, not expressions as they are in C: it helps detect and avoid mistakes, and it helps keep coding styles more consistent and readable.

Of course, it's also true that Lua's parser has to figure out where statements end based on what is or isn't a legal continuation of the previous statement. Expressions as statements would complicate this further.

/s/ Adam