Couldn't match expected type ‘Optional Decimal’ with actual type ‘Decimal’

Hey, DAML’ers! I am adjusting the Contract.daml for a scenario using the ex-cdm-swaps model, and need to specify the initialRate of the constructor FloatingRateSpecification.

initialValue is type ‘Decimal’. It accepts a decimal value without throwing an error. However, I get an error when I write a decimal for initialRate which is of type ‘Optional Decimal’.

What is the difference? How can I specify the decimal value without throwing an error? I noticed in the Utils.daml file here that there are helper functions to deal with types that require meta. Could I add something to that file?

Thanks for the help!

C:\Users\James Jaworski\OneDrive\Optimis\betaApp\third-test\Bloqbook-demo\daml\DA\swaps\Test\ExampleData\Contract.daml:285:59: error:
• Couldn't match expected type ‘Optional Decimal’
              with actual type ‘Decimal’
• In the ‘initialRate’ field of a record
  In the first argument of ‘Some’, namely
    ‘(FloatingRateSpecification
        {averagingMethod = None, capRateSchedule = [],
         finalRateRounding = None,
         floatingRateIndex = fieldWithEmptyMeta
                               FloatingRateIndexEnum_USD_LIBOR_ISDA,
         floatingRateMultiplierSchedule = None, floorRateSchedule = [],
         id = None, indexTenor = None, negativeInterestRateTreatment = None,
         rateTreatment = None,
         spreadSchedule = [SpreadSchedule
                             {id = None, initialValue = 0.005000, step = [], _type = None}],
         initialRate = 0.032753})’
  In the ‘floatingRate’ field of a record
                                rateSpecification =
                                RateSpecification with
                                    inflationRate = None
                                    floatingRate = Some
                                      (FloatingRateSpecification with
                                        averagingMethod = None
                                        capRateSchedule = []
                                        finalRateRounding = None
                                        floatingRateIndex = fieldWithEmptyMeta FloatingRateIndexEnum_USD_LIBOR_ISDA
                                        floatingRateMultiplierSchedule = None
                                        floorRateSchedule = []
                                        id = None
                                        indexTenor = None
                                        negativeInterestRateTreatment = None
                                        rateTreatment = None                                            
                                        spreadSchedule =  
                                          [SpreadSchedule with
                                            id = None
                                            initialValue = 0.005000
                                            step = []
                                            _type = None]
                                        initialRate = 0.032753
                                      )
                                    fixedRate = None

Optional Decimal is like a “wrapper” around Decimal that can have either Some value or None, where value would be of type Decimal. This allows you to clearly distinguish if a value is present or not.

In other languages this is often done using null, but that’s less explicit and brings its own set of problem (like NullPointerExceptions etc.).

1 Like

Okay cool. So I would specify an Optional Decimal like this initial value = Some 0.023756. Thanks for the clarification!

1 Like

Worth mentioning there are some helper functions defined in the DA.Optional module (docs here). You can use those via:

import DA.Optional
1 Like

Okay, neat. Thanks @georg!

1 Like

If you need to write DAML code which consumes an Optional value, the most general way of doing this is by pattern matching, introduced using the case keyword. For example:

myFunction : Optional Decimal -> Text
myFunction opt =
  case opt of
    None -> "I got nothing"
    Some value -> "I got a value: " <> show value

Even more succinctly, a pattern matching function can be written like this:

myFunction None = "I got nothing"
myFunction (Some value) = "I got a value: " <> show value
4 Likes