Multiple toclose on the same value..

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

Multiple toclose on the same value..

Francisco Olarte
One question regarding to close, just seeking confirmation ( or
denegation in case I've read the manual wrong ).

<toclose> depends on the variable going out of scope, so if I do
something like this ( I know example is stupid but something similar
may happen in more complex ways ):

do
  local original = generate_value_with_toclose_metatmethod()
  ...
  local <toclose> copy1 = original
  ...
  local <toclose> copy2 = original
  ...
end --

Will __toclose be called twice on the valuewhen reaching end ? ( I
expect it to be )

Same question with
do
   local <toclose> o = gvwtcmm()
   ...
   do
      local <toclose> c1 = o
      ...
   end
   ...
end

Will __toclose be called on reaching each end?

( 1st case is weird, but some code generation / value shuffling /
aliasing may lead to something like that, 2nd one may be more common
when reusing code via functions ( i.e., a function which does
something on an open connection closing it at end, a second function
which reads and acts upon some headers and dispatchs to the 1st one )
)

( How many times is called is not a problem to me, AAMOF I normally
dessign my "closeable" classes with an idempotent close() method, and
my plan to use <toclose> is just to alias it as __toclose, I just want
to know if I've interpreted the work manual correctly )

Regards.
   Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: Multiple toclose on the same value..

Coda Highland


On Sat, Jun 15, 2019 at 1:35 PM Francisco Olarte <[hidden email]> wrote:
One question regarding to close, just seeking confirmation ( or
denegation in case I've read the manual wrong ).

<toclose> depends on the variable going out of scope, so if I do
something like this ( I know example is stupid but something similar
may happen in more complex ways ):

do
  local original = generate_value_with_toclose_metatmethod()
  ...
  local <toclose> copy1 = original
  ...
  local <toclose> copy2 = original
  ...
end --

Will __toclose be called twice on the valuewhen reaching end ? ( I
expect it to be )

Same question with
do
   local <toclose> o = gvwtcmm()
   ...
   do
      local <toclose> c1 = o
      ...
   end
   ...
end

Will __toclose be called on reaching each end?

( 1st case is weird, but some code generation / value shuffling /
aliasing may lead to something like that, 2nd one may be more common
when reusing code via functions ( i.e., a function which does
something on an open connection closing it at end, a second function
which reads and acts upon some headers and dispatchs to the 1st one )
)

( How many times is called is not a problem to me, AAMOF I normally
dessign my "closeable" classes with an idempotent close() method, and
my plan to use <toclose> is just to alias it as __toclose, I just want
to know if I've interpreted the work manual correctly )

Regards.
   Francisco Olarte.


Based on the contract of <toclose> and it having been reiterated several times that it applies to the variable, not to the value, I think it's safe to assume that it will fire the __close metamethod twice in the first example and at the end of both scopes in the second example.

/s/ Adam
Reply | Threaded
Open this post in threaded view
|

Re: Multiple toclose on the same value..

Gabriel Bertilson
A simple testcase. Running it in the 5.4 interpreter (with newlines
removed) prints "closing" twice for me:

local x = setmetatable({}, { __close = function () print('closing') end })
local <toclose> copy1 = x
local <toclose> copy2 = x

— Gabriel



On Sat, Jun 15, 2019 at 5:03 PM Coda Highland <[hidden email]> wrote:

>
>
>
> On Sat, Jun 15, 2019 at 1:35 PM Francisco Olarte <[hidden email]> wrote:
>>
>> One question regarding to close, just seeking confirmation ( or
>> denegation in case I've read the manual wrong ).
>>
>> <toclose> depends on the variable going out of scope, so if I do
>> something like this ( I know example is stupid but something similar
>> may happen in more complex ways ):
>>
>> do
>>   local original = generate_value_with_toclose_metatmethod()
>>   ...
>>   local <toclose> copy1 = original
>>   ...
>>   local <toclose> copy2 = original
>>   ...
>> end --
>>
>> Will __toclose be called twice on the valuewhen reaching end ? ( I
>> expect it to be )
>>
>> Same question with
>> do
>>    local <toclose> o = gvwtcmm()
>>    ...
>>    do
>>       local <toclose> c1 = o
>>       ...
>>    end
>>    ...
>> end
>>
>> Will __toclose be called on reaching each end?
>>
>> ( 1st case is weird, but some code generation / value shuffling /
>> aliasing may lead to something like that, 2nd one may be more common
>> when reusing code via functions ( i.e., a function which does
>> something on an open connection closing it at end, a second function
>> which reads and acts upon some headers and dispatchs to the 1st one )
>> )
>>
>> ( How many times is called is not a problem to me, AAMOF I normally
>> dessign my "closeable" classes with an idempotent close() method, and
>> my plan to use <toclose> is just to alias it as __toclose, I just want
>> to know if I've interpreted the work manual correctly )
>>
>> Regards.
>>    Francisco Olarte.
>>
>
> Based on the contract of <toclose> and it having been reiterated several times that it applies to the variable, not to the value, I think it's safe to assume that it will fire the __close metamethod twice in the first example and at the end of both scopes in the second example.
>
> /s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Multiple toclose on the same value..

Francisco Olarte
In reply to this post by Coda Highland
Coda:
> Based on the contract of <toclose> and it having been reiterated several times that it applies to the variable, not to the value, I think it's safe to assume that it will fire the __close metamethod twice in the first example and at the end of both scopes in the second example.

That's what I interpreted too. Thanks for letting em know I'm not the only one.

Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: Multiple toclose on the same value..

Francisco Olarte
In reply to this post by Gabriel Bertilson
Gabriel:

On Sun, Jun 16, 2019 at 1:26 AM Gabriel Bertilson
<[hidden email]> wrote:
> A simple testcase. Running it in the 5.4 interpreter (with newlines
> removed) prints "closing" twice for me:
> local x = setmetatable({}, { __close = function () print('closing') end })
> local <toclose> copy1 = x
> local <toclose> copy2 = x

Thanks, I did a similar one, but I asked to make sure that there is no
"as toclose vars are const the compiler is free to optimize one of
them if it can prove they store the same value" hidden somewhere, as
it happened with my interpretation of object uncollectability, due to
some misguided interpretation of the manual. I'm nearlly sure current
version does that, but wanted to avoid future problems in my dessign.

Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: Multiple toclose on the same value..

Dibyendu Majumdar
In reply to this post by Francisco Olarte
On Sat, 15 Jun 2019 at 19:35, Francisco Olarte <[hidden email]> wrote:

>
> do
>   local original = generate_value_with_toclose_metatmethod()
>   ...
>   local <toclose> copy1 = original
>   ...
>   local <toclose> copy2 = original
>   ...
> end --
>
> Will __toclose be called twice on the valuewhen reaching end ? ( I
> expect it to be )
>

I haven't checked but the manual should state that the __close()
metamethod needs to be idempotent. It needs to track the state of the
resource so that if it is called multiple times, it still behaves
correctly.
This is a well known pattern that most close() operations need to follow.
Another aspect is that as is recommended in most other languages,
raising errors in finalizers and close operations is usually a bad
idea. Perhaps some advice on this should also be in the manual.

Regards

Reply | Threaded
Open this post in threaded view
|

Re: Multiple toclose on the same value..

Francisco Olarte
Dibyendu:

On Mon, Jun 17, 2019 at 11:16 AM Dibyendu Majumdar
<[hidden email]> wrote:
> I haven't checked but the manual should state that the __close()
> metamethod needs to be idempotent. It needs to track the state of the
> resource so that if it is called multiple times, it still behaves
> correctly.
> This is a well known pattern that most close() operations need to follow.
> Another aspect is that as is recommended in most other languages,
> raising errors in finalizers and close operations is usually a bad
> idea. Perhaps some advice on this should also be in the manual.

NEEDS is a little strong, specially since the <toclose> feature seems
to have an easy and well defined behaviour, and the <toclose>
x.__close() pairs are going to be nested unless someone does some
coroutine stuff ( I'm specifically planning on using it for
ref-counting on a handle I need to share among several coroutines, but
it is just going to substitute an old ref-counting mode ).

And the warning about erroring in finalizers and close methods is a
good hint too, but I think there is already something about it in the
manual, but I'll need to read it again, I'm not sure.

Francisco Olarte