Hugo, before looking at my suggestion, please take the time to fully understand how Lua module loading works. I plan to throw it all out and replace it with something completely different.
I want all module names to be complete URLs. This removes ambiguity about how and from where the module is loaded. But I will allow custom protocols to allow custom module loading.
Out of everything in the Package module, I will only keep Package.require and Package.loaded . Everything else will be removed. I will add Package.protocols which will be a map from a URL protocol to a loader function. The loader function will be passed the URL path and should return either the loaded module or nil if it fails.
I currently have some custom protocols for IO like "java" which is misnamed and should be "classpath". So the basic protocols would be "file" and "classpath". To load Java classes, the Java package would add a "class" protocol. To import ArrayList, you would do:
I would have a standard "luan" protocol defined like this:
return Package.protocols.classpath( "classpath:luan/modules/" .. path )
So to import the luan Table module:
The Web_server module could add a "site" protocol like this:
dir = dir.gsub("/$","") -- remove trailing '/' if any
return Package.protocols.classpath( "file:" .. dir .. path )
Then you could do:
My approach is more verbose than Lua but I think it is clearer.
I ended up adding protocols to Io instead of Package so that one can get an object for any protocol and do IO operations on it. The method to get the object is Io.get(url) but I am not sure about this name. The actual resource may not exists, so "get" may not be the right name. Maybe it should be Io.of(url) . What do you think is the right name?
I don't follow. Is this the method that is called when we try to import something? If yes, then users won't care about the name of this method because they only see an import statement, not a method call. Please explain.