Hi list,
I observed a weird bug, where an object instance x (of a class created with middleclass) fails to be identified as such using MyClass.isInstanceOf(x, MyClass) across modules.
i.e.:
-- Color.lua
local class = require 'middleclass'
local Color = class 'Color'
...
return Color
-- a_module.lua
local Color = require 'Color'
return {test = function(x) print(Color.isInstanceOf(x, Color) and 'is a Color' or 'is NOT a Color') end}
-- another_module.lua
local Color = require 'color' -- << notice the different case
local a_module = require 'a_module'
c = Color(...)
a_module.test(c) -- prints: is NOT a Color
This problem is catched very easily on Linux, since the filesystem is typically case-sensitive, and require'color' would fail.
But on Windows and macOS, which are case-insensitive, it goes unnoticed, and produces this apparently crazy behavior.
It all boils down to the fact:
> rawequal(require 'Color', require 'Color')
true
> rawequal(require 'Color', require 'color')
false
so if a module was loaded from a different file, for Lua it is a different module (fine) and this has repercussions on how middleclass checks for class appartenance (this is LESS fine, if one wants to maintain mental sanity).
Now, if the different name via which a module was loaded, differs from the canonical file system name, only in case (i.e. NOT in the actual file name, e.g. it could be a symlink, or location e.g. it could be a module with the same file name but in a different directory), then we are exactly in the crazy scenario where things might break very subtly.
One could monkey-patch require() (i.e. in pure Lua code) but it seems a lot more work, as one would have to replicate a lot of module search logic of the built-in require().
To me it would make sense for require() to warn or even fail if the require() arg doesn't match in case the actual filesystem name (especially when one works with multiple OSes, and it doesn't make sense that require() with mistyped arg (bad casing) works on one platform but fails on another).
What are your thoughts on this?
Cheers,
Federico Ferri