lua-users home
lua-l archive

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


Right, the 5.1 REPL doesn't print the values of expressions and you
therefore can't enter expressions on their own to see their values.
(They have to be preceded by an equals sign or "return", otherwise if
they can't be interpreted as statements, there is a syntax error.) I
found that very annoying since I'm used to the JavaScript console,
which allows you to do this. However, it is kind of counterintuitive,
in the REPL, that entering file:write() shows a return value.

As for documentation, the behavior of the Lua 5.3 REPL is documented
as follows[1]: "In interactive mode, Lua repeatedly prompts and waits
for a line. After reading a line, Lua first try to interpret the line
as an expression. If it succeeds, it prints its value. Otherwise, it
interprets the line as a statement. If you write an incomplete
statement, the interpreter waits for its completion by issuing a
different prompt. "

The 5.1 manual just includes the last sentence.[2]

The documentation of io.write in the 5.3 manual points you to
file:write(), whose documentation mentions the return value[3]: "In
case of success, this function returns file. Otherwise it returns nil
plus a string describing the error. "

But the Incompatibilities section for of the 5.3 manual doesn't
mention the change in the REPL,[4] and the 5.1 manual doesn't mention
the return value of file:write() (which seems to be a boolean).[5]

— Gabriel

[1] https://www.lua.org/manual/5.3/manual.html#7
[2] https://www.lua.org/manual/5.1/manual.html#6
[3] https://www.lua.org/manual/5.3/manual.html#pdf-file:write
[4] https://www.lua.org/manual/5.3/manual.html#8
[5] https://www.lua.org/manual/5.1/manual.html#pdf-file:write

— Gabriel



On Tue, Dec 31, 2019 at 5:37 PM Aaron Krister Johnson
<akjmicro@gmail.com> wrote:
>
> I guess that makes sense..? Will try later.
>
> I still contend that:
>
> 1) it doesn't work as advertised in the documentation. The docs do not mention this, that I have seen.
>
> 2) assuming that the above is true, the docs for lua5.3.* need updating, and this behavior needs explanation.
>
> 3) This behavior doesn't seem to be the case for the REPL prior to 5.3, per my statement that 5.1.5 worked as the docs state, w/o return values being printed.
>
> Personally, I like the old behavior better. :)
>
> -A
>
>
> On Tue, Dec 31, 2019, 3:07 PM nobody <nobody+lua-list@afra-berlin.de> wrote:
>>
>> On 31/12/2019 21.45, Aaron Krister Johnson wrote:
>> > Hi all,
>> >
>> > I am curious why 'io.write()' isn't behaving as advertised in lua5.3.*:
>> >
>> > ```
>> >  > io.write("A nice output msg")
>> > A nice output msgfile (0x7f8563013760)
>> >  >
>> > ```
>> >
>> > In particular, notice the "file (...)" bit that gets appended.
>>
>> io.write works correctly, it only looks wrong in the REPL.  This is
>> resulting from the interplay of several features:
>>
>> First, io.write always returns the file handle, so you can chain writes:
>>
>>    io.write "foo" :write "bar" :write "etc.…"
>>
>> Second, the REPL prints all return values.  'return 2+2' prints 4.
>> Saying 'return io.write "foo"' will result in exactly the behavior that
>> you observed. (This should also work in 5.1 IIRC?)
>>
>> Third, newer versions automatically try to prepend 'return ' to anything
>> you type at the REPL.  (So typing '2+2' no longer throws an error but
>> instead says '4'.)
>>
>> In combination,
>>
>>    > io.write "foo"
>>
>> will be read as 'return io.write "foo"', which prints "foo" and then the
>> return value(s) – in this case the file handle.
>>
>> Try putting a semicolon in front of your statement / expression.
>> 'return; io.write "foo"' isn't valid, which will result in it being
>> parsed as a statement (which doesn't return anything, and thus prints no
>> extra values).
>>
>> -- nobody
>>