How do I create an "Update [ContractId X]" from a list of template X?

Hi all,

I’m looking to refine my choice logic, to make it more elegant and hopefully a little more performant.

For the choice in question, we take in a list of product details, we need to process those details to create Product contracts for each, and the perform secondary logic to update the instance of ProductOwner with relevant Product information.

The intention was to do something like the below:

let processProductInformation(productInfo: ProductInformation) = do
    ...
    let newProduct = Product with ...
    return newProduct

let updateProductOwner(newProduct: Product) = do
    ...
    create ProductOwner with ...

let newProducts = map processProductInformation productsInfo
newProductOwners <- mapA updateProductOwner newProducts
newProductCids <- mapA create newProducts
return newProductCids

But I seem to have gone wrong with those last two lines, I’m trying to have the choice return [ContractId Product] but not certain exactly what’s needed to get there.

Any advice would be greatly appreciated!

It looks like processProductInformation already has type ProductionInformation -> Update Product. And you only use map here so newProducts : [Update Product]. You then try to mapA create over it which fails because create expects Product not Update Product.

To fix the issue try using a monadic bind and mapA:


newProducts <- mapA processProductInformation productsInfo
newProductOwners <- mapA updateProductOwner newProducts
newProductCids <- mapA create newProducts
return newProductCids

Note that newProductOwners is unused which seems a bit suspicious.

You’re right on newProductOwners, that was my mistake when writing the question, I am in fact using mapA_ in my DAML.

I tried your suggestion, and now the IDE says I’m returning Update (Update [ContractId Product]) rather than the expected Update [ContractId Product]

That sounds like you are using a let instead of a monadic bind <- for newProductCids maybe?

Okay, that was my mistake again, it warned me of redundant return, so I tried return (mapA create newProducts) whereas all I really needed was mapA create newProducts as the last line.

Thanks for that!

2 Likes