Chapter 2: Intermediate

Lesson 4: Match

Based on the condition in the previous lesson, there could be two outputs: True or False.

For each of these outputs there should be a different response. If the key is present then our program should prohibit user from trying to register again, but if it doesn't exist then the user should be allowed by the contract to register.

This is very similar to the If-Else structure in programming. However, in Scilla we use pattern matching because of the flexibility and additional functionalities inherent in pattern matching.

This pattern matching is done with the keyword match. Here variableName (without [ ] ) is the variable against which we are running the test. Based on the different possible values of this variable, we want different blocks of code to be executed.

After deciding the variable that we're running the checks for, we need to code the patterns that we will be matching it against. The pattern to be matched can be a variable binding, an ADT or the wildcard symbol of underscore _ which matches against anything.

Naturally, if used, the wild card should be last in the series of the patterns being matched. If it is used first, everything will successfully match against it and the other patterns will never be checked at all.

Finally the end keyword declares the end of the patterns. Any variables declared in the statements for a particular match clause are valid only within that clause. Their values won't be externally recognised i.e. the scope of a variable declared within a pattern in a match condition is limited to that clause.

In this example, if the check variable is true, we set the value of the variable a equal to variable b or else if it's false, then we set it to be equal to the variable c. Note that we assume that the variables check, a, b and c have all been previously defined.

We have a task for you!

In the transition deposit, make a matchthat compares sender_is_owner with two branches, True and False

You can leave the body blank for now.

Close the match condition properly with end.

Your Workspace

Show Solution

Solution

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

scilla_version 0
import BoolUtils
contract SocialMediaPayment (owner: ByStr20)
transition deposit()
sender_is_owner = builtin eq _sender owner;
(* Start typing from the line below. *)
match sender_is_owner with
| False =>
| True =>
end
end
scilla_version 0
import BoolUtils

contract SocialMediaPayment (owner: ByStr20)

transition deposit()
  sender_is_owner = builtin eq _sender owner
  (* Start typing from the line below. *)

end
1
2
3
4
5
6
7
8
9
10