lua-users home
lua-l archive

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

On Fri, Jun 18, 2021 at 12:27 PM Andrew Gierth
<> wrote:
> Arguably the best thing to do here would be to make "local function"
> imply <const> automatically; if you want the name to be a mutable local
> var, then you can use
> local x
> x = function() end

That breaks some code ( mine ) ( Normally my local functions are
(logically) const, but I've just checked at least two of my modules
use a strategy function inside the module which gets changed when the
module changes state, it can be trivially fixed for that, but it has
to be )

( would not matter too much for me as I track changes and I would
preemptively change the affected code, but it may have nasty side
effects elsewhere, I use local function a lot, and only pinpointed
those two this fast because I worked on the module last week, I would
need to scan all the code or just do 'perl -I.bak -s
/local\s+function\s*(\w+)/local $1; $1 = function /'   )

I also would dislike having explicit <const> in "local x" and default
in "local function". And having "local function a" be const and "local
a = function" be non const. I would much prefer to have a "local
<const> function" cosntruct in the case I need a self-referential
const function.

Also, original report ( local x const + function x overwriting it )is
has nothing to do about making local functions const, IIRC it is a bug
which seems reasonable easy to fix ( probably a missing const check in
some code path  ).

One thing I've noticed is that, contrary to other languages, lua
syntax allow uninitialized const declarations ( local x <const>;,
treated as =nil the same as when no const used ). Things will get
complicated but using that as a forward declaration could make some
things easier ( like mutually recursive const functions, do "local
f<const>; local function g<const> something_using_f; local function
f<const> something using g". But this would be a complicated and
complicating change not to be attacked lightly, and only after more
experience with const usage is gained, it is in its infance as proved
by the original bug. I nearly prefer juggling functions manually for
the couple of places where I could "benefit" from it ).

Francisco Olarte.