wybe
wybe copied to clipboard
Allow resources with the same name to be imported from different modules and used in statements/expressions
This will permit a much more satisfactory fix for #414. Until then, procs that use the multiple resources with the same resource name (and different modules) can't be supported, and that includes any module that imports multiple modules that each define initialised resources with the same name and has top-level code.
An alternative to this could be to support aliases for resources. Something like this:
use resource foo.res as foo_res
use resource bar.res as bar_res
def proc use !foo_res, !bar_res in {
pass
}
This would also be beneficial for module imports too, I think.
Implementation wise, an extra normalisation pass would be required, transforming resource aliases into their fully qualified names, and then replacing any resource-variable reference with that alias as a reference to said resource.
That's a great idea! I'll change the title of this issue.
I'm thinking also, we could do this at the proc level:
def proc use !foo.res as foo_res, !bar.res as foo_res {
pass
}
It has the benefit of being easier to parse (as we don't have a new top-level item to parse, and can also be extended to use blocks:
use foo.res as foo_res in {
pass
}
but it has the downside of being repeated everywhere the resource is used if there's a conflict in that proc/use block.
Now that I'm thinking about it, though, I like my original syntax better. It has the benefit of being applicable to more than just resources, potentially giving aliases for procs and types 👀
One of the principles of resources is to name things consistently. Of course, the case under discussion is where we can't do that due to conflict, but I think it's still desirable in this case to be as consistent as we can. In this case, that's file-level consistency, which your first suggestion provides.
I'm not sure if it's really warranted, but we could generalise this idea to cover procs and functions and even types by providing a syntax to rename things as they are imported. Maybe generalise the existing from ... use ...
syntax, something like
from foo.bar use foobar_res = res, fbfunc = func, t = type
to use the name foobar_res
for the resource foo.bar.res
, use fbfunc
to call foo.bar.func
, and use the name t
for the type type
.
I like the idea of having aliases for anything that can be exported. Using from
would introduce a new keyword (which isn't necessarily a bad thing), but I think we could do with with less. Something like
use module (alias = submodule.item, other = type)
I assume that this aliasing would have to be restricted to aliasing all items that have the same name (synonymous procs with differing arities/modes), unless we introduce some syntax for that, a la
use module (inout = proc(+,-))