Prefix both libraries to avoid name collision
- Leonid_RozenbergOP
In reading this section on handling name collisions, in the import section, I am confused by why we need to prefix both packages? Is
import Foo1.X as X1 import X as X2ambiguous ?
- cocreature 1
The difference is in your example you have
Foo1.XandXso they are different module names. In the docs both modules are calledX. If you import it unqualified damlc does not know which one it should use. - Leonid_Rozenberg
Sorry, I think that I am missing a step in the process. When I import something with
asdoesn’t that change the name?Would
import Foo1.X qualified as X1 import X qualified as X2still be ambiguous?
- Gary_Verhaegen
So there are a couple things going on here.
First off, the linked documentation page attempts to address the case where you depend on two packages that both export the same module. In all of your code snippets here, you’re using syntax that assumes modules already have different names, but export the same symbols. I’m assuming you know this, but for the benefit of other readers let’s delve into it a little bit more.
In your case (different modules with same-name symbols), yes, you can use
assyntax to disambiguate. Note that when youimport M1 as M2, you import all of the symbols ofM1both “naked” and qualified. This is useful if you generally prefer using unqualified names, but you have maybe one or two symbols in that package that conflict with other packages.When you
import qualified M1 as M2(orimport M1 qualified as M2, which is equivalent), you only import the qualified variants of the symbols inM1, so they’re not available in their “naked” form.It’s probably important to point out that Daml, inheriting that behaviour from Haskell, will fail on ambiguous use of a multiple-times-imported symbol, not when importing it. So if you have two modules,
M1definingf1andf2, andM2definingf1andf3, the following code will not produce an error on the ambiguousf1, because it’s not used:import M1 import M2 a = undefined f2 b = undefined f3This would fail to compile:
import M1 import M2 a = undefined f2 b = undefined f3 c = undefined f1 -- FAIL: ambiguous reference to f1, could mean M1 or M2.And this would work:
import M1 as M1 import M2 as M2 a = undefined f2 b = undefined f3 c = undefined M1.f1 d = undefined M2.f1Now, the documentation you linked is about two packages exporting the same module. In that case you need an extra level: in your
daml.yamlfile, you can give a name to the package itself, and then use that name in theimportstatement as an extra string argument. By default, packages use their own name, which will work in cases where the packages have different names (you could depend on two versions of the same package, in which case they’ll have the same name, and then you can use special syntax in thedaml.yamlfile to give them different names).So your question is really at the
daml.yamllevel. If I may rephrase it: assuming you are depending on two versions of the same package foo, would the followingdaml.yamlbe enough to avoid conflicts?build-options: - '--package' - 'foo-1.0.0 with (X as Foo1.X)' - '--package' - 'foo-2.0.0'Presumably this would define the
Xmodule fromfoo-1.0.0into theFoo1.Xmodule for local compilation, and theXmodule fromfoo-2.0.0would be defined as plainX, so it looks like there’s no conflict. I’m afraid I don’t know the answer there, but I hope I’ve at least clarified the question for more knowledgeable people. (@cocreature ?) - cocreature
Take a look at 7.9. Packages — Glasgow Haskell Compiler <release> Users Guide. Renaming using
withis additive so you still get a collision using the unqualified name. However, you can use it without thewithto avoid that. - Leonid_Rozenberg
@Gary_Verhaegen Amazing answer! And you’re right I am thinking at the package import level.
I think I’ve figured out one subtlety; the
build-options--packagerenaming can only be used on a per module basis. So it would be inefficient to use it to apply to all modules in a package. Which is what themodule-prefixesaccomplishes?Another related question, do
build-optionsandmodule-prefixesapply only todata-dependenciesordependenciestoo? - cocreature
module-prefixesis just syntactic sugar for renaming all modules in a package so purely for convenience.Both
build-optionsandmodule-prefixesapply to deps & data-deps although I strongly recommend only using data-deps. If there is a reason that forces you to use deps, it’s probably a good idea to raise an issue before this becomes an issue for you once you upgrade SDKs.