Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using pl.Map with keys equal to method names #188

Open
ghost opened this issue Mar 6, 2016 · 3 comments
Open

Using pl.Map with keys equal to method names #188

ghost opened this issue Mar 6, 2016 · 3 comments

Comments

@ghost
Copy link

ghost commented Mar 6, 2016

This might not be a real bug report. It could easily be my fault in that I'm using pl.Map in the wrong way.

Consider the following code:

local Map=require"pl.Map"
local m=Map()
m:set("get", 5)
m:get("get")

This fails with the error

lua: /home/local/test/test.lua:48: attempt to call a number value (method 'get')

That's not surprising: the key "get" shadows the method. But does this mean that the map is now unusable (at least the "get" method) until "get" is deleted?

@stevedonovan
Copy link
Contributor

Yes, it's a known problem, because the lookup cannot distinguish between the table and the metatable __index. Now we could make this bullet proof at the cost of performance - make __index a function and put some logic in it. For pl.Set I resolved the issue by having no methods.

@stevedonovan
Copy link
Contributor

I'll leave this open, but I'm concerned about the performance implications.

@joshuahhh
Copy link

Would it work for the Map table to store its key-value pairs in a separate table, like self.contents? That way the contents would never conflict with the methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants