Thoughts on new syntax for toclose

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

Thoughts on new syntax for toclose

Dibyendu Majumdar
The latest commits show the syntax <toclose> being adopted instead of *toclose.

Aesthetically I find the new syntax to be nicer, but this still has
the problem that the previous syntax had; which is that the symbol '<'
has meaning in expressions therefore using this symbol (like the *
symbol) may restrict the ability to use extensions inside expressions.
Of course extensions will always be restricted to local declarations
in which case it doesn't matter.

Regards
Dibyendu

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Sean Conner
It was thus said that the Great Dibyendu Majumdar once stated:
> The latest commits show the syntax <toclose> being adopted instead of *toclose.

  There's also <const> as well.  But you can't combine them:

        do
          local <toclose> f = io.open(foo) -- okay
          local <const>   x = 5 -- okay
          local <toclose> <const> g = io.open(bar) -- error
          local <const> <toclose> h = io.open(baz) -- also error
        end

> Aesthetically I find the new syntax to be nicer, but this still has
> the problem that the previous syntax had; which is that the symbol '<'
> has meaning in expressions therefore using this symbol (like the *
> symbol) may restrict the ability to use extensions inside expressions.
> Of course extensions will always be restricted to local declarations
> in which case it doesn't matter.

        do
          local *toclose f = io.open(foo)
          local *const   x = 5
        end

        do
          local @toclose f = io.open(foo)
          local @const   x = 5
        end

  Eh.  I tend to prefer the '@' syntax, but only because '@' has no current
use in Lua.  But I can live with this.

  But the latest manual for Lua 5.4 still mentions the old syntax.

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Sean Conner
In reply to this post by Dibyendu Majumdar

  <toclose> could be more useful.  In the past, I used to have this
function:

        local function dir(path)
          local dir = fsys.opendir(path)
          return function(d)
            return d:next()
          end,dir
        end

But that would keep the directory open until the next garbage collection and
sometimes, that wasn't enough to keep from exhausting the open file limit.
The code now will close the directory upon reaching the end:

        local function dir(path)
          local dir = fsys.opendir(path)
          return function(d)
            local e = d:next()
            if not e then d:__gc() end
            return e
          end,dir
        end

It would be nice to have the __close method called at the end of the
interation, but alas, this:

        local function dir(path)
          local <toclose> dir = fsys.opendir(path)
          return function(d)
            return d:next()
          end,dir
        end

does not work---the directory is *immediately* closed before the iteration
starts.  It's in the context of iterators that I would love to use
<toclose>.

  -spc

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Sergey Zakharchenko
In reply to this post by Sean Conner
Sean,

>   There's also <const> as well.  But you can't combine them:

Sources say you needn't:

https://github.com/lua/lua/blob/d9f40e3f6fb61650240c47d548bee69b24b07859/lparser.c#L1679

> var->ro = 1; /* to-be-closed variables are always read-only */

Supposedly for future extensions several such annotations would be
placed inside the angle brackets, comma-separated or something... just
guessing.

Best regards,

--
DoubleF

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Philippe Verdy
In reply to this post by Sean Conner
For the use case outside expressions, the "<" is supposed to be disambiguated by the previous token which is a reserved declarator token ("local" at start of a statement). If there was annotations to use in expressions, we would have then to prefix that notation with a declarator token.
I really don't see the interest of not using a common convention that just uses a single character.
Note that with the addition of the ">", it supposes that there may be parameters after the annotation token (only "toclose", or "const" for now). But I still see a use of such annotations inb expressions, notably for debugging info, optimization hints, floatting point evaluation mode (strict or relaxed mode, signaling or non-signaling NaNs, IEEE rounding mode...), control of parallelism or multithreading (relaxed or strict evaluation order of subexpressions, notably with function calls, or with operands of commutative or associative operations, or for allowing local declaration inside expressions with a limitation of scope (e.g in "(local a=1)+a": where does the scope of a terminates ?)

Controling the scope of "toclose" variables in expressions is also useful, to determine more precisely when the finalizer is allowed to run.

Or may be this can be controled by allowing blocks of instructions to have a return value, by enclosing them in parentheses and using the return statement at end of block, such as:
   x = (begin local a = 1; return a end) + 2 --[[ mostly equivalent  to x=3 ]].
In that case we can use statement starting with declarators like "local" optionnaly fowllowed by one or more annotations:
   x = (begin local <const> a = 1; return a end) + 2 --[[ mostly equivalent  to x=3 ]].


Le mer. 22 mai 2019 à 05:09, Sean Conner <[hidden email]> a écrit :
It was thus said that the Great Dibyendu Majumdar once stated:
> The latest commits show the syntax <toclose> being adopted instead of *toclose.

  There's also <const> as well.  But you can't combine them:

        do
          local <toclose> f = io.open(foo)      -- okay
          local <const>   x = 5                 -- okay
          local <toclose> <const> g = io.open(bar) -- error
          local <const> <toclose> h = io.open(baz) -- also error
        end

> Aesthetically I find the new syntax to be nicer, but this still has
> the problem that the previous syntax had; which is that the symbol '<'
> has meaning in expressions therefore using this symbol (like the *
> symbol) may restrict the ability to use extensions inside expressions.
> Of course extensions will always be restricted to local declarations
> in which case it doesn't matter.

        do
          local *toclose f = io.open(foo)
          local *const   x = 5
        end

        do
          local @toclose f = io.open(foo)
          local @const   x = 5
        end

  Eh.  I tend to prefer the '@' syntax, but only because '@' has no current
use in Lua.  But I can live with this.

  But the latest manual for Lua 5.4 still mentions the old syntax.

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Roberto Ierusalimschy
In reply to this post by Sergey Zakharchenko
> >   There's also <const> as well.  But you can't combine them:
>
> Sources say you needn't:
>
> https://github.com/lua/lua/blob/d9f40e3f6fb61650240c47d548bee69b24b07859/lparser.c#L1679

The manual also says that:

  3.3.8 – To-be-closed Variables
  [...]
  A to-be-closed variable behaves like a constant local variable, [...]
                                     
-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Roberto Ierusalimschy
In reply to this post by Sean Conner
>   But the latest manual for Lua 5.4 still mentions the old syntax.

Where exactly?

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Sean Conner
It was thus said that the Great Roberto Ierusalimschy once stated:
> >   But the latest manual for Lua 5.4 still mentions the old syntax.
>
> Where exactly?

  Section 3.3.8 and 8.3.

  -spc (It also doesn't mention <const>)


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Kenneth Lorber
In reply to this post by Dibyendu Majumdar


On May 22, 2019, at 10:59 AM, [hidden email] wrote:

Message: 3
Date: Wed, 22 May 2019 10:11:41 -0300
From: Roberto Ierusalimschy <[hidden email]>
Subject: Re: Thoughts on new syntax for toclose
To: Lua mailing list <[hidden email]>
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=utf-8

 There's also <const> as well.  But you can't combine them:

Sources say you needn't:

https://github.com/lua/lua/blob/d9f40e3f6fb61650240c47d548bee69b24b07859/lparser.c#L1679

The manual also says that:

 3.3.8 – To-be-closed Variables
 [...]
 A to-be-closed variable behaves like a constant local variable, [...]

-- Roberto

Doesn't that make <const> redundant (and somewhat confusing)?

Dropping <const> would then require setting the __close metamethod to nil to get the desired behavior, which would quickly get ugly - but isn't that also an issue for <toclose> variables?

So letting my imagination run free, how about these for inspiration:
local foo <const> = 3
local foo <{__close=myclosefoo}> = some_expression
local foo <{__close=myclosefoo, __tostring=prettyprint}> = some_table

In other words, call setmetatable from the declaration, which would convert the two separate statements needed now into one, which seems cleaner to me.
Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Roberto Ierusalimschy
In reply to this post by Sean Conner
> It was thus said that the Great Roberto Ierusalimschy once stated:
> > >   But the latest manual for Lua 5.4 still mentions the old syntax.
> >
> > Where exactly?
>
>   Section 3.3.8 and 8.3.
>
>   -spc (It also doesn't mention <const>)

Where is this "latest" manual?

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Sean Conner
It was thus said that the Great Roberto Ierusalimschy once stated:

> > It was thus said that the Great Roberto Ierusalimschy once stated:
> > > >   But the latest manual for Lua 5.4 still mentions the old syntax.
> > >
> > > Where exactly?
> >
> >   Section 3.3.8 and 8.3.
> >
> >   -spc (It also doesn't mention <const>)
>
> Where is this "latest" manual?

  My mistake, I forgot to regenerate the manual.

  The code base I'm using is from github:

        https://github.com/lua/lua/

  Sorry for the noise.

  -spc

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Xavier Wang
In reply to this post by Dibyendu Majumdar


Dibyendu Majumdar <[hidden email]> 于2019年5月22日周三 上午7:47写道:
The latest commits show the syntax <toclose> being adopted instead of *toclose.

Aesthetically I find the new syntax to be nicer, but this still has
the problem that the previous syntax had; which is that the symbol '<'
has meaning in expressions therefore using this symbol (like the *
symbol) may restrict the ability to use extensions inside expressions.
Of course extensions will always be restricted to local declarations
in which case it doesn't matter.

Regards
Dibyendu

I have a new idea about the syntax of <toclose>, what about this?

local function get_content(filename)
    local f: toclose = assert(io.open(filename, "rb"))
    return f:read "*a"
end

just use :, it's just like the type annotation, and it opens a way to makes Lua support (optional) type declarations.

--
regards,
Xavier Wang.
Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on new syntax for toclose

Dibyendu Majumdar
On Sun, 2 Jun 2019 at 15:14, Xavier Wang <[hidden email]> wrote:

> Dibyendu Majumdar <[hidden email]> 于2019年5月22日周三 上午7:47写道:
>>
>> The latest commits show the syntax <toclose> being adopted instead of *toclose.
>>
>> Aesthetically I find the new syntax to be nicer, but this still has
>> the problem that the previous syntax had; which is that the symbol '<'
>> has meaning in expressions therefore using this symbol (like the *
>> symbol) may restrict the ability to use extensions inside expressions.
>> Of course extensions will always be restricted to local declarations
>> in which case it doesn't matter.
>>
> I have a new idea about the syntax of <toclose>, what about this?
>
> local function get_content(filename)
>     local f: toclose = assert(io.open(filename, "rb"))
>     return f:read "*a"
> end
>
> just use :, it's just like the type annotation, and it opens a way to makes Lua support (optional) type declarations.
>

Hi, its been suggested before. Syntactically it is nicer and familiar
as other languages use similar syntax. But ':' is not unambiguous in
expressions as it is used as self operator, so it has the same
drawback as '<>' in that respect.

Regards
Dibyendu