[Proposal] rawpairs()

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

[Proposal] rawpairs()

Soni "They/Them" L.
pairs() is considered harmful, and can cause bugs:
https://github.com/MightyPirates/OpenComputers/issues/1625

Now, `for k, v in next, t do` is a bit less clear than `for k, v in
pairs(t) do`, so my proposal is that we get a built-in rawpairs(), as
`for k, v in rawpairs(t) do` is clear, and it'd be good for new users to
not be confused when they see it but it fails on their interpreter with
"attempt to call nil".

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Sean Conner
It was thus said that the Great Soni L. once stated:
> pairs() is considered harmful, and can cause bugs:
> https://github.com/MightyPirates/OpenComputers/issues/1625

  Could not reproduce.

[spc]lucy:/tmp>cat y.lua
local t = {}
local mt = {
  __index = {"1", "2", "3", test="hi"}, -- index
  __gc = function() end -- dummy __gc, to trigger bug
}
local mtmt = {
  __pairs = function() -- dummy __pairs that returns an empty iterator
    return next, {}, nil
  end
}
setmetatable(mt, mtmt) -- needs to be done first
setmetatable(t, mt) -- trigger bug
-- ok so now let's test our __index shall we?
print(t[1], t[2], t[3], t.test) -- nil, nil, nil, nil
-- wait what?! it doesn't work!

[spc]lucy:/tmp>lua-51 y.lua
1 2 3 hi
[spc]lucy:/tmp>lua-52 y.lua
1 2 3 hi
[spc]lucy:/tmp>lua-53 y.lua
1 2 3 hi
[spc]lucy:/tmp>

  My Lua 5.1 and 5.2 are the latest version, but I think I'm still a bit
behind on 5.3 (5.3.0), so that may have some effect.  But otherwise ... what
exactly is going on?

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Duncan Cross
On Tue, Jan 26, 2016 at 6:11 PM, Sean Conner <[hidden email]> wrote:
> It was thus said that the Great Soni L. once stated:
>> pairs() is considered harmful, and can cause bugs:
>> https://github.com/MightyPirates/OpenComputers/issues/1625
>
>   Could not reproduce.

(...)

>   My Lua 5.1 and 5.2 are the latest version, but I think I'm still a bit
> behind on 5.3 (5.3.0), so that may have some effect.  But otherwise ... what
> exactly is going on?
>
>   -spc

It looks like the OpenComputers project uses a sandbox that overrides
setmetatable() with a custom-written wrapper, that involves use of
pairs().

Search for 'setmetatable =', here:
https://github.com/MightyPirates/OpenComputers/blob/master-MC1.7.10/src/main/resources/assets/opencomputers/lua/machine.lua

(Disclaimer: I don't know anything about the OpenComputers project,
this is the first I've heard of it. If I have misunderstood the
situation, I apologise.)

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Tim Hill
In reply to this post by Soni "They/Them" L.

> On Jan 26, 2016, at 7:50 AM, Soni L. <[hidden email]> wrote:
>
> pairs() is considered harmful, and can cause bugs: https://github.com/MightyPirates/OpenComputers/issues/1625
>
> Now, `for k, v in next, t do` is a bit less clear than `for k, v in pairs(t) do`, so my proposal is that we get a built-in rawpairs(), as `for k, v in rawpairs(t) do` is clear, and it'd be good for new users to not be confused when they see it but it fails on their interpreter with "attempt to call nil”.
>

Just rename the article “OpenComputers project considered buggy” and everything is ok.

—Tim




Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Dirk Laurie-2
In reply to this post by Soni "They/Them" L.
2016-01-26 17:50 GMT+02:00 Soni L. <[hidden email]>:

> `for k, v in next, t do` is a bit less clear than `for k, v in pairs(t) do`

On the contrary, I regard it as an idiom for bypassing __pairs
metamethods and have often used it.

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Soni "They/Them" L.


On 26/01/16 08:02 PM, Dirk Laurie wrote:
> 2016-01-26 17:50 GMT+02:00 Soni L. <[hidden email]>:
>
>> `for k, v in next, t do` is a bit less clear than `for k, v in pairs(t) do`
> On the contrary, I regard it as an idiom for bypassing __pairs
> metamethods and have often used it.
>
It is not newbie-friendly and a newbie would probably be confused by it.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Coda Highland
On Tue, Jan 26, 2016 at 2:07 PM, Soni L. <[hidden email]> wrote:

>
>
> On 26/01/16 08:02 PM, Dirk Laurie wrote:
>>
>> 2016-01-26 17:50 GMT+02:00 Soni L. <[hidden email]>:
>>
>>> `for k, v in next, t do` is a bit less clear than `for k, v in pairs(t)
>>> do`
>>
>> On the contrary, I regard it as an idiom for bypassing __pairs
>> metamethods and have often used it.
>>
> It is not newbie-friendly and a newbie would probably be confused by it.
>

Under what circumstances is a newbie going to be dealing with __pairs
metamethods and a specific need to bypass them? That's something of a
meta-level operation that you don't generally do when writing
high-level code, and newbies are typically writing high-level code.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Duncan Cross
In reply to this post by Soni "They/Them" L.
On Tue, Jan 26, 2016 at 3:50 PM, Soni L. <[hidden email]> wrote:
> pairs() is considered harmful, and can cause bugs:
> https://github.com/MightyPirates/OpenComputers/issues/1625

By the way, it's worth noting that "Considered Harmful" is an
infamous, powerful phrase in programming because of Dijkstra's
recommendation to thoroughly reject "goto" -- a deep-rooted orthodoxy,
which the Lua core team has already demonstrated they're quite happy
to ignore. ;)

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Sergey Zakharchenko
In reply to this post by Dirk Laurie-2
2016-01-27 1:02 GMT+03:00 Dirk Laurie <[hidden email]>:
> 2016-01-26 17:50 GMT+02:00 Soni L. <[hidden email]>:
>
>> `for k, v in next, t do` is a bit less clear than `for k, v in pairs(t) do`
>
> On the contrary, I regard it as an idiom for bypassing __pairs
> metamethods and have often used it.

I have just noticed this behavior (I mainly use Lua 5.1 so this is not
too important for me) and I'm somewhat disappointed. I'm used to
having functions bypassing metatables named 'raw*',and be regarded as
'unsafe' in a way; I welcomed the change in pairs/ipairs/etc. to
respect metatables and the fact that 'next' still bypasses them is a
bit of a pity. I guess it's a bit late to change it though...

Best regards,

--
DoubleF

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] rawpairs()

Rena
In reply to this post by Duncan Cross

On Jan 26, 2016 5:59 PM, "Duncan Cross" <[hidden email]> wrote:
>
> On Tue, Jan 26, 2016 at 3:50 PM, Soni L. <[hidden email]> wrote:
> > pairs() is considered harmful, and can cause bugs:
> > https://github.com/MightyPirates/OpenComputers/issues/1625
>
> By the way, it's worth noting that "Considered Harmful" is an
> infamous, powerful phrase in programming because of Dijkstra's
> recommendation to thoroughly reject "goto" -- a deep-rooted orthodoxy,
> which the Lua core team has already demonstrated they're quite happy
> to ignore. ;)
>
> -Duncan
>

See also: "Considered harmful" considered harmful: http://meyerweb.com/eric/comment/chech.html
So many people seem to think they can sway public opinion by just including these two magic words, that I tend to ignore such remarks.