# Optional Int to Int ? (Casting: is it possible?)

**URL:** <https://forum.canton.network/t/optional-int-to-int-casting-is-it-possible/6633>\
**Category:** App Development\
**Tags:** daml\
**Created:** [June 2, 2023, 5:05pm UTC](https://forum.canton.network/t/optional-int-to-int-casting-is-it-possible/6633 "2023-06-02T17:05:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessewermert](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jessewermert](https://forum.canton.network/u/jessewermert)\
**Post date:** [June 2, 2023, 5:05pm UTC](https://forum.canton.network/t/optional-int-to-int-casting-is-it-possible/6633/1 "2023-06-02T17:05:21Z")

</div>

Is there a way to turn an Optional Int type to an Int type in Daml?

```haskell
if (winningParticipant == betPayload.participantToWin)
          then do  
            let
              -- the below creates the following: (["Giants", "Jets"], [0.25, 1.13]) 
              (evtParticipants, evtParticipantOdds) = unzip eventPayload.eventRecord.predictions
              -- Get the index of the winning participant to use it to index into the evtParticipantOdds list
              winningParticipantIndex = elemIndex winningParticipant evtParticipants
              -- Get the odds of the winning participant to determine payoutRate
              payoutRate = (!!) evtParticipantOdds winningParticipantIndex
              -- Get the payout amt
              payoutAmt = betPayload.betSlip.wager * payoutRate

```

The error we are receiving is the following: " Couldn’t match expected type ‘Int’  
with actual type ‘Optional Int’  
• In the second argument of ‘(!!)’, namely  
‘winningParticipantIndex’  
In the expression: (!!) evtParticipantOdds winningParticipantIndex  
In an equation for ‘payoutRate’:  
payoutRate = (!!) evtParticipantOdds winningParticipantIndextypecheck ".

In the code, we are trying to get the odds number of the winning participant of a given bet, however, when we are trying to get this using the (!!) to index into evtParticipantOdds using the winingParticipantIndex, however that returns a type of Optional Int, rather than a Int. Any suggestions?

---

<div class="post-metadata">

**Author:** ![jessewermert](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jessewermert](https://forum.canton.network/u/jessewermert)\
**Post date:** [June 2, 2023, 7:40pm UTC](https://forum.canton.network/t/optional-int-to-int-casting-is-it-possible/6633/2 "2023-06-02T19:40:39Z")

</div>

@gabmora

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [June 2, 2023, 8:31pm UTC](https://forum.canton.network/t/optional-int-to-int-casting-is-it-possible/6633/3 "2023-06-02T20:31:20Z")

</div>

Daml doesn’t really have a concept of a cast but instead there are explicit functions to convert. Before we look at those, let’s take a look at how you can do without any library functions:

Let’s say `x` has value `Option Int`, you can always pattern match on it:

```haskell
case x of
  None -> -- do something here
  Some y -> -- do something with y

```

what exactly you want to do there depends on your usecase ofc. One option would be to just call `error` in the `None` case which will abort the transaction. If you are not familiar with pattern matching, take a look at the [docs](https://docs.daml.com/daml/intro/3_Data.html#variants-and-pattern-matching)

Pattern matching is great but sometimes a bit more verbose than one would like. That’s where library functions can help.

There are three main ones that are useful here:

[`fromOptional`](https://docs.daml.com/daml/stdlib/DA-Optional.html#function-da-optional-fromoptional-77879) takes an Optional and a default value and gives you the default value if the Optional was None or the value of the optional otherwise, e.g.,

```haskell
fromOptional None 0 == 0
fromOptional (Some 5) 0 == 5

```

[`fromSomeNote`](https://docs.daml.com/daml/stdlib/DA-Optional.html#function-da-optional-fromsomenote-25463) takes an error message and an optional and aborts your transaction with the error message if it was `None`, e.g.,

```haskell
fromSomeNote "value X was not present" None

```

Lastly, there is [`fromSome`](https://docs.daml.com/daml/stdlib/DA-Optional.html#function-da-optional-fromsome-59859). It’s like `fromSomeNote` but doesn’t allow you to customize the error message. it can be convenient during testing but I would recommend to avoid it in production code. The lack of a good error message can often make it very hard to debug why your transaction gets aborted if you hit the `None` case.

---

<div class="post-metadata">

**Author:** ![jessewermert](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jessewermert](https://forum.canton.network/u/jessewermert)\
**Post date:** [June 2, 2023, 9:50pm UTC](https://forum.canton.network/t/optional-int-to-int-casting-is-it-possible/6633/4 "2023-06-02T21:50:40Z")

</div>

Thanks very much for reply. Will look into this now. Very much appreciated @cocreature !
