# How to ignore diverging types in an if/else expression?

**URL:** <https://forum.canton.network/t/how-to-ignore-diverging-types-in-an-if-else-expression/4487>\
**Category:** App Development\
**Tags:** daml\
**Created:** [May 6, 2022, 1:27pm UTC](https://forum.canton.network/t/how-to-ignore-diverging-types-in-an-if-else-expression/4487 "2022-05-06T13:27:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbatko](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/pbatko/32/2368_2.png) [@pbatko](https://forum.canton.network/u/pbatko)\
**Post date:** [May 6, 2022, 1:27pm UTC](https://forum.canton.network/t/how-to-ignore-diverging-types-in-an-if-else-expression/4487/1 "2022-05-06T13:27:25Z")

</div>

The following piece of code fails to typecheck with the following message:

```haskell
    • Couldn't match type ‘B’ with ‘A’
      Expected type: Update (ContractId A)
        Actual type: Update (ContractId B)
    • In the expression: create (B s)
      In a stmt of a 'do' block:
        _ <- if flag then create (A s) else create (B s)
      In the expression:
        do if flag then create (A s) else create (B s)
           return ()typecheck

```

```haskell
template A
    with
        s: Party
    where
        signatory s

template B
    with
        s: Party
    where
        signatory s

template Helper
    with 
        s: Party
    where
        signatory s

        nonconsuming choice DoIt: ()
            with
                flag: Bool
            controller s
            do
                if flag then
                    create (A s)
                else   
                    create (B s)
                return ()

```

I do not care about return values of neither `create A` and `create B`.  
What’d be a clean way to make this code compile?

My current workaround is:

```haskell
template Helper
    with 
        s: Party
    where
        signatory s

        nonconsuming choice DoIt: ()
            with
                flag: Bool
            controller s
            do
                if flag then
                    (create (A s), return ())._2
                else   
                    (create (B s), return ())._2
                return ()

```

---

<div class="post-metadata">

**Author:** ![pbatko](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/pbatko/32/2368_2.png) [@pbatko](https://forum.canton.network/u/pbatko)\
**Post date:** [May 6, 2022, 1:35pm UTC](https://forum.canton.network/t/how-to-ignore-diverging-types-in-an-if-else-expression/4487/2 "2022-05-06T13:35:48Z")

</div>

I’ve found a nicer way:

```haskell
template Helper
    with 
        s: Party
    where
        signatory s

        nonconsuming choice DoIt: ()
            with
                flag: Bool
            controller s
            do
                if flag then
                    do
                        create (A s)
                        return ()
                else   
                    do
                        create (B s)
                        return ()

```

---

<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:** [May 6, 2022, 1:36pm UTC](https://forum.canton.network/t/how-to-ignore-diverging-types-in-an-if-else-expression/4487/3 "2022-05-06T13:36:57Z")

</div>

The most efficient way and also the simplest is something like this

```haskell
if flag 
  then
    fmap (\_ -> ()) (create (A s))
  else
    fmap (\_ -> ()) (create (B s))

```

We can then simplify that in two steps: First for the special case of `fmap (\_ -> a)` we can use `a <$`

```haskell
if flag 
  then
    () <$ create (A s)
  else
    () <$ create (B s)

```

And then for the special case of `() <$` there is a functoin called `void`:

```haskell

if flag 
  then
    void $ create (A s)
  else
    void $ create (B s)

```

---

<div class="post-metadata">

**Author:** ![Sofia\_Faro](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sofia_faro/32/168_2.png) [@Sofia\_Faro](https://forum.canton.network/u/Sofia_Faro)\
**Post date:** [May 6, 2022, 2:37pm UTC](https://forum.canton.network/t/how-to-ignore-diverging-types-in-an-if-else-expression/4487/4 "2022-05-06T14:37:11Z")

</div>

Hi @pbatko,

I want to second @cocreature’s approach of using `void` here to discard the return value of an action:

```haskell
  if flag then
      void $ create (A s)
  else
      void $ create (B s)

```

But I also want to point out that the first workaround you suggested:

```haskell
  if flag then
      (create (A s), return ())._2
  else   
      (create (B s), return ())._2

```

Does not work as intended. Those `create` actions are never executed, only the `return` actions.

In Daml, if you have an expression of the form `(a, b)._2` then both `a` and `b` will be evaluated as expressions, but only `b` will be executed as an action. This is a consequence of Daml being a purely functional programming language, like Haskell, where simply evaluating an expression does not cause any side effect (like the effect of “create”) to happen.

To actually execute a sequence of actions in Daml, the actions have to be linked together using `do` notation. So the right way to evaluate and execute two actions, while ignoring the result of the first, is by using `do` notation, like in your second suggestion:

```haskell
  do 
     a
     b

```

Alternatively, you can use the [`>>` operator](https://docs.daml.com/daml/stdlib/Prelude.html#function-da-internal-prelude-gtgt-56231), which uses the same mechanism as `do` notation to link two actions together:

```haskell
     a >> b

```
