We can also declare variables within a transition. Please note that these variables are not immutable variables nor are they mutable variables like the ones we learned about earlier even though the values of these variables that exist within a transition can also be changed. This is because their value is never stored on the blockchain. Rather they are temporary variables whose scope is limited within the transition for the duration of that single call.
The temporary variable declaration format is different from the format in which the mutable variables are declared.
For instance, a String variable is declared in the given format.
Note: In a transition, if there are multiple lines, then they are separated by semicolons
. Let’s take a look at an example of a transition with two variables declared in it:
In this example, we declare two variables in a transition named foo
. The first variable is a string
type variable that stores the value hello
. The second variable is an integer
type variable that stores the value 5
.
Notice two points in this example:
b
because there are no further lines in the transition seen in the example. Those who are familiar with other computer languages might be used to using a semicolon to end a line of code. However, here the semicolon can be thought of as being used in the capacity of a conjunction rather than a period, so just like we won’t normally use the conjunction ‘and’ at the end of a sentence, similarly we don’t use the semicolon here at the end but instead to join the various lines.We want to replace the original user name declared in mutable variable username
in this transition. To do that, we will need a new name.
Declare a new variable with the name newname
and type String
and give it a value Bob
.
Show Solution
scilla_version 0
contract SocialMediaPayment (owner: ByStr20)
field username: String = "Alice"
transition changeName()
(* Start typing from the line below *)
end
12345678