lua-users home
lua-l archive

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


2016-03-02 21:22 GMT+02:00 Soni L. <fakedme@gmail.com>:
> So I took a quick glance at the lua.c (interpreter) source and found out you
> could do this:
>
> $ lua
> Lua 5.3.2  Copyright (C) 1994-2015 Lua.org, PUC-Rio
>> _PROMPT="~> "
> ~> _PROMPT="=> "
> => _PROMPT2="~> "
> => do
> ~> end
> =>
>
> The Lua 5.3 manual says, about names, "As a convention, programs should
> avoid creating names that start with an underscore followed by one or more
> uppercase letters (such as _VERSION).", and about the registry, "As with
> variable names, string keys starting with an underscore followed by
> uppercase letters are reserved for Lua.", and the Lua interpreter/REPL is a
> program.

It happens to be a program called "lua". I'd say that particular program
is entitled to certain privileges of the first-born.

This seems to be a perfect demonstration of the principle. 'lua' has
need of variable names that users are supposed not to use for their
own purposes. Underscore-capital names are reserved for that
purpose.

_PROMPT and _PROMPT2 are
names of the designated pattern, not defined, but will be used if someone
defines them.

> Does this mean it's always safe to use the globals _PROMPT and
> _PROMPT2? Or should the REPL be moved into a preloaded "repl" library
> instead?

It is never safe to use any global of that form. The manual tells
you not to.

$ lua
Lua 5.3.2  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> _PROMPT = "Would you like to enter some Lua code? "
Would you like to enter some Lua code? _PROMPT2 = "Please add to the
current chunk. "
Would you like to enter some Lua code? function s(x,y)
Please add to the current chunk. return x+y
Please add to the current chunk. end
Would you like to enter some Lua code?

> Also why isn't this documented anywhere?

At last, a valid point. It was documented up to Lua 5.1.5.
Its omission was probably a slip of the mouse.