Managing namespaces in Daml
- LucianoOP 13
Shortly after joining DA, I was fortunate to have the opportunity of working on a large client project making heavy use of Daml. I’d like to share one of the software engineering approaches I learnt, which I think every budding Damler runs into sooner or later: handling namespaces.
In an object-oriented language like Java, class methods will have namespaces assigned automatically. For instance:
class Cat { boolean fur; }In the above snippte
furinherits theCatnamespace, so you can use a fully qualified name like so:Cat#fur. Not so in Daml or Haskell.You’ve probably run into a problem like this at some point:
module Main where template MarriageProposal with proposer: Party proposee: Party where signatory proposer observer proposee controller proposee can Accept: Marriage do undefined template Marriage ... template BusinessProposal with proposer: Party proposee: Party where signatory proposer observer proposee controller proposee can Accept: Business do undefined -- parse error! template Business ...This module fails to compile. The reason is that there are two definitions of
Accept, which live in the sameMainnamespace. A typical workaround for this is to prefix the definition with it’s templates name e.g.MarriageAcceptandBusinessAcceptto avoid the clash.This still leaves another, similar problem: When importing these templates into another module, the fields
proposerandproposeeare again clashing. Prefixing everything can lead to really messy code.The way to solve this properly is through the use of qualified imports. Split your
Mainmodule into two files:Marriage.damlandBusiness.daml, structured like this:module Marriage where type T = Marriage template Proposal with proposer: Party proposee: Party where signatory proposer observer proposee controller proposee can Accept: Marriage do undefined template Marriage -- ...Do the same for
Business.You can now import these into a third file explicitly, like so:
import qualified Marriage (Proposal, Accept, T) import qualified Business (Proposal, Accept, T) getMarried : Script Marriage.T getMarried = do cid <- submit bob $ createCmd $ Marriage.Proposal bob alice submit alice $ exerciseCmd cid Marriage.Accept getRich : Script Business.T getRich = do cid <- submit alice $ createCmd $ Business.Proposal alice charlie submit charlie $ exerciseCmd cid Business.AcceptYou’ll notice the use of the alias
Tto avoid having to write e.g.Marriage.Marrigein type definitions.It can get a bit cumbersome if you want to use a constructor; in this case, I tend to just add a second import for the unqualified constructor name e.g.
import Marriage (Marriage)so that I don’t need to writeMarriage.Marriage with ...This approach lets you use both modules by specifying their namespace explicitly, and as you can see is very similar to the syntax granted by OO languages. In cases where there’s no ambiguity, you can just drop the
qualifiedmodifier.It also plays surprisingly well with generated code outside the ledger e.g. typescript bindings.
To close, I’ll suggest that if you find this useful, you should also learn about explicit export lists (i.e.
module Main (Marriage, Business) where ...instead of justmodule Main where ...). They let you emulatepublic/privatemember access of OO languages: just omit private functions from the export list.Hope this was helpful. Comments welcome!
- gyorgybalazsi 3
Another Haskell descendant, Elm uses a similar approach.
In Elm, you have eg a
mapfunction in the List package and in the String package.The List version:
map : (a -> b) -> List a -> List bThe String version:
map : (Char -> Char) -> String -> StringIf you want to use both functions in a file, it’s best to use them in a qualified way you have described as
List.mapandString.map. - Luciano 2
Yes, that’s a good point. I also use
qualifiedto avoid clashes for data structures such asSet.toList,Map.toListor the more generalFoldable.toList. There are lots of these functions that are named the same. - quidagis
Hi @Luciano this is what I was searching for just now

Based on others answers to order and formatting that I have seen & received, would I be correct in stating that the order of qualified imports is upto the application writer?
One of my application sections, has 7 templates, with very similar actions therefore I have learned the hard way, that the use of the same term like
messageSenderin multiple locations cause dissonance.For the sake of Logic flow, would you/anyone tend to set the first discrete section/function as the prime document, and then import into there, or perhaps choose the most important section as the prime document?
- Luciano
quidagis:would I be correct in stating that the order of qualified imports is upto the application writer?
Assuming I’ve understood you correctly here, do you mean:
import qualified Marriage (Proposal, Accept, T) import qualified Business (Proposal, Accept, T)vs
import qualified Business (Proposal, Accept, T) import qualified Marriage (Proposal, Accept, T)Yes, I do believe that there is no difference in which order you put these. One approach is to sort these alphabetically. I think somebody even wrote a tool for doing this to Daml programs … it’s linked somewhere in the forum.
- quidagis
Ah, my poor English, I meant:
File: JointVenture.daml
module JointVenture where import qualified Marriage (Proposal, Accept, T) import qualified Business (Proposal, Accept, T)Or File: Marriage.daml
module Marriage where import qualified JointVenture (Proposal, Accept, T) import qualified Business (Proposal, Accept, T)And so on …
- Luciano 1
quidagis:tend to set the first discrete section/function as the prime document, and then import into there,
Here I’m not entirely sure I understand what you’re asking.
Perhaps an example of how I structure my code helps:
When writing a (non-pure) daml program, I start by writing down business logic in a template, with choices left as
undefined. The key thing here is that each choice has typically multiplecontrollers. This means that you won’t be able to exercise these through the API directly, as you won’t have multiple signatures (although you can test this in ascriptusingsubmitMulti).As a second step I write what I think of as “workflow” contracts in another package, which takes care of first collecting signatures and then calling these multi-party choices in the business contract. The workflows tend to be
accept/propsestyle contracts typically, and that’s where I’ll have the name clashes, so they’re the ones I import qualified, leaving my core business logic as the ‘top level’ module. That’s the idea of the approach:- business logic stays in contracts with multiple-controller choices.
- workflows are imported into the business module, and can be made up of one or more templates, with single controllers, to collect signatures. These can be called extrernally from the API.
I think this also answers your first question above.
- quidagis
Agree, thank you very much.
I had not considered that method as you detailed,
it is quite elegant and logical. - quidagis
I used this method to import the Doctor module into another Module, like this
module Doctor where type T = Doctor -- Error message ...module AnotherModule where -- Import Doctor module for PrescriptAuth function -- import qualified Doctor (CommenceConsult, ContinueConsult, CompleteConsult, PrescriptAuth, T) ...From your example you import the Templates, which leads me to ask, do you have to import all of the Templates in that Module, or just the ones you need?
In my example I am getting an error message indication on the
Doctorrecord type, Error message below:/home/quid/Projects/daml/code/getmednz/daml/Doctor.daml:6:10: error: Not in scope: type constructor or class ‘Doctor’typecheckWhy?
- cocreature 3
Type synonyms reference a type but based on your example it looks like you haven’t defined a type called
Doctorwhich is why you get the error. If you look at the example Luciano wrote you can see that theMarriagemodule also defines aMarriagetemplate. - quidagis
I’ll re-read & adjust, after I have a working UI

- Stephen
When you write
import qualified Marriagewith no import list, you get all the [exported] symbols. Unlike normalimport, where you’re merging all imported symbols into the unqualified namespace, there’s rarely a reason to list imports at all, since the origin of the symbol is already obvious according to each reference location. That is, because each reference looks likeMarriage.Proposalin the importing file, we get no value from the fact thatProposalwas listed with the import.The exception to this is if you’re using the somewhat obscure feature of merging multiple qualified imports to a single qualifier:
import qualified Foo as F import qualified Bar as FThis might call for an import list for the same reason that unqualified imports call for an import list: you’re merging multiple modules’ contents to a single qualifier.