Chapter 1: Elementary

Lesson 7: Getting values

You’ll notice the issue with the previous lesson that the new user is always changed to a fixed value each time that the transition is called. Ideally, we’ll like to have options other than “Bob” about the new name which is updated. This can be done by letting the user pass on a value to the transition whenever she calls it.

Similar to how we can pass variables to the contract while creating it, we can also pass variables to a transition while calling it.

In case we want to declare multiple variables here, then we can do so by separating the variable name and variable types by commas.

1

2

3

transition transitionName (parameterName_1: parameterType_1)
end

We have a task for you!

We want to change the name of the user Alice that we’ve earlier stored in the mutable variable username. To do this, we created the transition changeName and assigned a value to username. In order to make the change more flexible, we’ll also need the new name each time the transition is called. So, we will delete the old variable declaration in the body of the transition.

Include the parameter with name newname and variable type String in the declaration of the transition so that a user has to send a new name for the user each time she wishes to change the old value.

Your Workspace

Show Solution

Solution

1

2

3

4

5

6

7

8

9

scilla_version 0
contract SocialMediaPayment (owner: ByStr20)
field username: String = "Alice"
(* Start typing in the parentheses below *)
transition changeName(newname: String)
username := newname
end
scilla_version 0

contract SocialMediaPayment (owner: ByStr20)
field username: String = "Alice"

(* Start typing in the parentheses below *)
transition changeName()
  username := newname 
end
1
2
3
4
5
6
7
8
9