DA.List vs DA.List.Total
App Development4 posts248 views1 likesLast activity Apr 2023
NE
Neelam_DwivediOP
Apr 2023I see the difference between DA.List and DA.List.Total functions as the use of Optional, e.g.
tail: [a] → [a]
tail: [a] → Optional [a]
Are there any other differences apart from the use of Optional?
CO
cocreature
Apr 2023DA.List throws exceptions for some functions when they are not defined, e.g., tail on an empty list. DA.List.Total instead returns an Optional for that as you pointed out. That’s the only difference here.
NE
Neelam_Dwivedi
Apr 2023Thanks @cocreature !
AN
Andrae
Apr 2023I would also recommend preferring either DA.List.Total or DA.NonEmpty over DA.List in production code. The Daml type system does a fantastic job of helping you avoid bugs if you stick to correct-by-construction datatypes and avoid non-total functions wherever possible (ie. functions that throw exceptions).
For example:
- If you know there must always be at least one element in the list. This means
head : [a] -> acan’t fail, so you can encode that in the “correct-by-construction”NonEmptydatatype and use(.hd) : NonEmpty a -> ainstead. - Alternatively, using
DA.List.Totalwill guarantee that you correctly handle the “oops, that list was empty after all” case as the code won’t allow you to access theainSome aunless you also handle theNonecase.