lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Thu, Jun 5, 2014 at 10:08 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
I doubt that is happening. Two equal functions will be equal forever.

(What can happen (and is happening) is for the same code sometimes
create two equal functions and sometimes create two different ones.)

Okay. I see. In busted, perhaps because it is making use of debug functions, two lines of code that were right next to each other would frequently result in different functions references between `a` and `b`. Outside of busted, this did not seem to happen when they were adjacent, but happened 100% of the time if I called `collectgarbage` in between.

But given you statement that you just made, the following code and results are not surprising:

```lua 

local url = "">

local a = url("mcp://host/thread/name")
local b = url("mcp://host/thread/name")

print(a == b)
--> true
collectgarbage() 
print(a == b)
--> true 
print(getmetatable(a).__eq == getmetatable(b).__eq)
--> true

local a = url("mcp://host/thread/name")
collectgarbage()
local b = url("mcp://host/thread/name")
print(a == b)
--> false

print(getmetatable(a).__eq == getmetatable(b).__eq)
--> false

```

In real code, the creation of two objects would happen at different times and so this would be more inconsistent .

Again, this quote from the reference manual...

> Closures with the same reference are always equal. Closures with any detectable difference (different behavior, different definition) are always different.

... allows for the current behavior. More precisely, it almost explicitly avoids addressing this scenario.

I respect the trade-offs, and this behavior for the `getequalhandler` inconsistent in a way that is hard to spot and hard to debug, even if the fix is simple, once found. 

I've already re-factored my code, so I'll just have to watch out for this 'gotcha'.

Thank you for considering this.

-Andrew