[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Another option for closing variables
- From: Sergey Kovalev <kovserg33@...>
- Date: Wed, 10 Jul 2019 00:23:41 +0300
вт, 9 июл. 2019 г. в 19:44, Dirk Laurie <dirk.laurie@gmail.com>:
>
> Op Ma. 8 Jul. 2019 om 19:33 het Gé Weijers <ge@weijers.org> geskryf:
>
> > I think the "<toclose>" facility is a fair tradeoff between added language complexity and expressive power. It's also likely to be much faster because there's no need to create closures, no need to pack or unpack parameter lists, and no need to use 'pcall' or forward the error.
>
> The VM instructions in lopcodes.c are
>
> opmode(0, 0, 0, 0, iABC) /* OP_TBC */
> opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */
>
> So 'local <toclose> x = initializer' generates the same code as 'local
> x = initlalizer', plus ' TBC lx' where lx is the number of the local
> variable x, plus 'CLOSE lx' at the end of the scope.
>
> Given that there is to be a name for the attribute that x should have,
> only two bytes '<>' of syntax is used up, and those bytes provide the
> feature of associating other attributes with a local variable.
>
> Personally (as I have argued in another post) I think the name
> 'close', with metamethod '__toclose', too specific, since it is merely
> a method called when a local name with which that variable is
> associated goes out of scope. There is no need at all for the value
> itself to end its existence: it could have other names. 'close' is
> appropriate for a file, but the value really could be anything, e.g. a
> custom message handler.
>
Syntax for toclose is very inconsistent. For example defer
implementation in lua 5.4:
defer=setmetatable({},{__sub=function(t,f)
return setmetatable({},{__close=f})
end})
usage:
do
local<toclose>_=defer-function() print "defer" end
print "body"
end
output:
body
defer
1. I have to declare name.
2. Syntax is inconsistent. It looks like squashed alien:
"local ‘<’ name ‘>’ Name ‘=’ exp"
only one name and must be one expression.
local <toclose> name=123 -- will cause error but it after end of block
and if no error in block
local <toclose> name -- error need expression
local <toclose> name=nil -- fine. will work without error
The way it could be used is to raw. And should be found more suitable syntax.
ps: local <const> a={} a.x=1 print(a.x)
What is this constants for? Area where they could be helpful is too limited.
Isolate block or function from up-values will be much useful. This
could solve problem of isolating code from current context.