[NoW] Once more about to-be-closed variables

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: [NoW] Once more about to-be-closed variables

Sergey Kovalev
Yes indeed. There is no pure function in lua. That's why they are quite rate.
upvalues are almost everywhere that's we are unable to isolate
different part of code.
You have to declare all used variables as local to ensure there is no
side effect, but if you forget to declare some variable
the behaviour will depends on context. Even if we override _ENV
variable to disable access to globals, but we can't disable upvalues.
The only solution is to use "load" function
https://pastebin.com/9jrvzR33 but it isn't perfect.

чт, 4 июл. 2019 г. в 08:01, Egor Skriptunoff <[hidden email]>:

>
> On Thu, Jul 4, 2019 at 12:47 AM Sergey Kovalev wrote:
>>
>>
>> If we would have pure function declaration this problem causes less pain.
>> Pure function like ordinary function but isolated from global and
>> up-values.
>
>
> Pure functions are quite rare.
> Upvalues are almost everywhere in Lua :-)
>

Reply | Threaded
Open this post in threaded view
|

Re: [NoW] Once more about to-be-closed variables

Egor Skriptunoff-2
On Thu, Jul 4, 2019 at 3:43 PM Sergey Kovalev wrote:
Yes indeed. There is no pure function in lua. That's why they are quite rate.
upvalues are almost everywhere that's we are unable to isolate
different part of code.



Nested scopes (with ability to access variables/functions from outer scope but not from inner) is a useful feature.
Nested scopes was a strong argument in "Pascal vs. C" holywars, and I was very proud to be at Pascal side :-)

Lua programmers are using upvalues frequently because upvalues are powerful; that's why pure functions are rare in Lua.

Reply | Threaded
Open this post in threaded view
|

Re: [NoW] Once more about to-be-closed variables

Sergey Kovalev
пт, 12 июл. 2019 г. в 00:10, Egor Skriptunoff <[hidden email]>:

>
> On Thu, Jul 4, 2019 at 3:43 PM Sergey Kovalev wrote:
>>
>> Yes indeed. There is no pure function in lua. That's why they are quite rate.
>> upvalues are almost everywhere that's we are unable to isolate
>> different part of code.
>>
>
>
> Nested scopes (with ability to access variables/functions from outer scope but not from inner) is a useful feature.
> Nested scopes was a strong argument in "Pascal vs. C" holywars, and I was very proud to be at Pascal side :-)
>
> Lua programmers are using upvalues frequently because upvalues are powerful; that's why pure functions are rare in Lua.
Upvalues are powerful but some times you need ability to isolate one
part of code from another. The ability to isolate all upvalues in some
scopes could be very handful.

fn=pure_function(args)
  body -- here all upvalues deined outsize is absent
end
Now it could be achieved only using load function.
https://raw.githubusercontent.com/kov-serg/lua-aux/master/pure.lua
But this method is unable to force declare all variables used
explicitly. Instead it could return all "global" variable it change.

Another side effects are in modules:
[ex1.lua]
print("x",x)

[main.lua]
x=1 require "ex1"
package.loaded.ex1=nil
x=2 require "ex1"

Such sides effects could be headache in big projects. Because result
will depend from order of requires. Instead modules should be defines
in isolated environments.
The ability to localize or prevent side effects could be more useful
than badly thought-out toclose syntax.

12