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”,
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:
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:
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,