Safe DA.Date functions

Is there a reason why the functions in DA.Date for creating a Date aren’t total? I was surprised to find there’s nothing with type f : (Int, Month, Int) -> Maybe Date, and I don’t believe there’s any way to catch the error in fromGregorian.

3 Likes

Hi @Axman6-da, in general the DAML standard library is not particularly religious about trying to keep everything total and instead opts for a pragmatic approach of making the choice on a per-definition basis depending on whether we expect users to want to handle the error case or not.

I don’t know the original reasoning for making fromGregorian non-total but based on the code I’ve seen, it is rarely used on dynamic user input (for that, let users pass in a Date instead of validating in DAML) and instead used for static values or at least values not under user control where the error case is often not super useful.

You do raise a good point about not being able to catch the error if you do need to handle it. We are currently working on adding exception handling to DAML which will allow you to catch this and other errors. In the meantime, you will have to check the preconditions yourself before calling fromGregorian.

2 Likes