Strong tables in Lua 5.4

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

Re: Strong tables in Lua 5.4

Eric Man-3
What was the reason we don't use:

setmetatable(t, { __mode = 'strong' })

to mark table as strong?

On Tue, Mar 27, 2018 at 12:25 PM, Italo Maia <[hidden email]> wrote:
So, does "missing"/"undefined" singleton for handling holes in tables have a verdict? Seems quite compatible and easy to handle in old/new code.

2018-03-21 14:10 GMT-03:00 Hisham <[hidden email]>:
On 21 March 2018 at 13:06, Roberto Ierusalimschy <[hidden email]> wrote:
> Many thanks for the examples. All of them actually would raise errors in
> my little experiment, but let us see them in the discussion about nils
> in tables.

I hope this shows how easy it is to find examples of the construct I
was talking about. :)

> In the examples from Hisham, I could not figure out whether we should see
> the nils as actual values or "instructions" to not add something.
> For instance, in the case of
>
>   entity[k] = default_value(field)
>
> does a nil mean no default or a nil as the default? Would it make any
> difference to the program if the nil was added to the table?

In both cases it is an instruction to not add something. In
default_value(), because we have to interact with the outside world
(converting to and from JSON), when the default is "null" we return it
as ngx.null.

And I hope this shows how non-trivial it is to audit working codebases
regarding their usage of nil. :)

-- Hisham




--
"A arrogância é a arma dos fracos."

===========================
Me. Italo Moreira Campelo Maia
Co-fundador do Grupo de Usuários Python do Ceará
Secretário ForHacker (fb.com/ForHackerSpace)
Desenvolvedor Full-Stack, Escritor, Empresário, Visionário
-----------------------------------------------------
Meu Livro, Site, Blog
===========================

Reply | Threaded
Open this post in threaded view
|

Re: Strong tables in Lua 5.4

Dirk Laurie-2
2018-03-27 5:21 GMT+02:00 Eric Man <[hidden email]>:

> What was the reason we don't use:
>
> setmetatable(t, { __mode = 'strong' })

It uses too many letters.

At present 'k' means keys are weak, 'v' means values are weak. Just
's' or maybe 'S' (strong feels as it it should be capitalized) would
be enough.

Reply | Threaded
Open this post in threaded view
|

Re: Strong tables in Lua 5.4

Sean Conner
It was thus said that the Great Dirk Laurie once stated:

> 2018-03-27 5:21 GMT+02:00 Eric Man <[hidden email]>:
>
> > What was the reason we don't use:
> >
> > setmetatable(t, { __mode = 'strong' })
>
> It uses too many letters.
>
> At present 'k' means keys are weak, 'v' means values are weak. Just
> 's' or maybe 'S' (strong feels as it it should be capitalized) would
> be enough.

  So what would this mean?

        setmetatable{t,{ __mode = "vS" })

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: Strong tables in Lua 5.4

Eric Man-3
In reply to this post by Dirk Laurie-2
Yes "strong"is too long - what I meant was your original proposal of using setmetatable and __mode - it morphed into "make every table a strong one" so I wanted to bring the discussion back to this; I think it's a good proposal and I haven't read any good technical arguments against it yet.

On Tue, Mar 27, 2018 at 5:31 PM, Dirk Laurie <[hidden email]> wrote:
2018-03-27 5:21 GMT+02:00 Eric Man <[hidden email]>:

> What was the reason we don't use:
>
> setmetatable(t, { __mode = 'strong' })

It uses too many letters.

At present 'k' means keys are weak, 'v' means values are weak. Just
's' or maybe 'S' (strong feels as it it should be capitalized) would
be enough.


Reply | Threaded
Open this post in threaded view
|

Re: Strong tables in Lua 5.4

pocomane
On Tue, Mar 27, 2018 at 12:58 PM, Eric Man <[hidden email]> wrote:
> Yes "strong"is too long - what I meant was your original proposal of using
> setmetatable and __mode - it morphed into "make every table a strong one" so
> I wanted to bring the discussion back to this; I think it's a good proposal
> and I haven't read any good technical arguments against it yet.
>

Let  `set_strong_mode` be a wrapper to set the metamethod to anything
we need to make it strong. Then consider the following code:

```
local tab = {}
local key = {}
tab[key] = true
set_strong_mode(tab)
tab[key] = nil
key = nil
```

Now, how I can delete the table content?

Yes you can suppose that when set back to "Normal" mode, the key
should be collected, But this means that the set of a metamethod field
should trigger a table iteration. I did not find yet a nice way to
solve this issue...

Reply | Threaded
Open this post in threaded view
|

Re: Strong tables in Lua 5.4

pocomane
On Tue, Mar 27, 2018 at 2:30 PM, pocomane <[hidden email]> wrote:

>
> ```
> local tab = {}
> local key = {}
> tab[key] = true
> set_strong_mode(tab)
> tab[key] = nil
> key = nil
> ```
>
> Now, how I can delete the table content?

Well, actually this is the same behaviour also for the normal lua
tables. I do not why it did seem strange to me in case of
strong-tables.

However, sorry for the noise :)

123