# How to use if...else for just assigning values

**URL:** <https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435>\
**Category:** App Development\
**Tags:** daml\
**Created:** [April 27, 2022, 9:55am UTC](https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435 "2022-04-27T09:55:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [April 27, 2022, 9:55am UTC](https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435/1 "2022-04-27T09:55:50Z")

</div>

G-d willing

Hello,  
I have a very basic question that can help me in not duplicating lines of code.

Let’s say I have a boolean flag that determines a name of a customer.  
For example, if the value is true then the name should be “A” else the name should be “B”,

```haskell
setup : Script ()
setup = script do
  let val = True
  if val then do
    let firstName = "A"
    return ()
  else do
    let firstName = "B"
    return ()

  debug firstName

```

However, the scope of firstName ends once we go out of the if…else’ scope. And it indicates that the line `debug firstName `is having a compilation error saying:

```haskell
error:
    Variable not in scope: firstName

```

And if I try to introduce the firstName before the if…else block then the value is not getting updated:

```haskell
setup : Script ()
setup = script do
  let val = True
      firstName = "C"
  if val then do
    let firstName = "A"
    return ()
  else do
    let firstName = "B"
    return ()

  debug firstName

```

The output of the debug is “C”.  
Is this solvable? How can I overcome this?

I am aware that I can create a function that checks this value and return the correct value, however, my scenario is more complicated than this, since I have a data type with many variables and types, and depending on the condition, not all variables need to be changed. I mean the data structure has variables a, b, c & d. if the condition is true, variables a, b & d should get updated, else variables b, c & d should be updated. In addition, following my example, variable d is defined as Either, and depending on the condition it will be set with a different data type.

Thanks,

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [April 27, 2022, 10:10am UTC](https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435/2 "2022-04-27T10:10:36Z")

</div>

Hi @cohen.avraham,

“Variables” in Daml are immutable, meaning that you cannot change their value, ever. We usually call them “bindings” instead of “variables” to underline that. All you can do is create new bindings, and they are usually scoped to the `do` block enclosing them.

So when you write:

```haskell
setup : Script ()
setup = script do
  let val = True
  if val then do
    let firstName = "A"
    return ()
  else do
    let firstName = "B"
    return ()

  debug firstName

```

you have two separate `firstName` bindings, each in their own, separate scope, and there is no `firstName` binding in the scope of the last line, hence the error.

In

```haskell
setup : Script ()
setup = script do
  let val = True
      firstName = "C"
  if val then do
    let firstName = "A"
    return ()
  else do
    let firstName = "B"
    return ()

  debug firstName

```

you have three scopes and three separate `firstName` bindings. The line

```haskell
let firstName = "A"

```

is **not** changing the value of `firstName` to be `A` _instead of_ `C`. What it is doing is introducing a **new** binding `firstName` in the current scope, which _shadows_ (renders inaccessible) the preexisting `firstName` binding from the parent scope. But only in the current scope, i.e. the current `do` bloc; once that is finished, the original `firstName` binding is again accessible, and that’s why it prints `A`.

So how do we work with immutable bindings? If we can’t have statements that change the value of variables, the only tool we have is to _return_ values. Fortunately, `if` is an expression that returns a value. So for this simple case you can just write:

```haskell
setup : Script ()
setup = script do
  let val = True
  let firstName = if val then "A" else "B"
  debug firstName

```

Given your description of your real use-case (thanks for posting a simplified version, by the way, that makes it a lot easier), you may need to restructure things a bit to fit the constraint that bindings are immutable.

---

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [April 27, 2022, 11:58am UTC](https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435/3 "2022-04-27T11:58:58Z")

</div>

> [@Gary\_Verhaegen](#):
>
> ```haskell
> let firstName = if val then "A" else "B"
> 
> ```

G-d willing

I know that in Daml everything is mutable.  
Anyway, thanks, @Gary_Verhaegen for that solution, this is a better solution so far (compared to using a function).  
The only “problem” now will be, me typing a lot of that specific “if” condition. But, as said, it is a better way than calling a function, and it is more readable.

The only thing it does not answer is the multiple assignments which, as you said, will need to be restructured or I will need to do a lot of typing

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [April 27, 2022, 12:42pm UTC](https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435/4 "2022-04-27T12:42:07Z")

</div>

For the multiple assignment issue, there are a few possible solutions. It’s hard to give a recommendation without knowing more about your code; I’d recommend opening a new question thread with a more concrete example of what you’re trying to achieve.

---

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [April 27, 2022, 12:55pm UTC](https://forum.canton.network/t/how-to-use-if-else-for-just-assigning-values/4435/5 "2022-04-27T12:55:43Z")

</div>

will do
