Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationMatch statements

Official ZPE/YASS documentationMatch statements

ZPE 1.9.1 (Kelso, February 2021) added support for the match construct. Whilst it's syntax is similar to that of a function, match is not a function.

The match statement is a very powerful, high-performance statement designed to be used much like an inline when-is statement, allowing both a concise, easy to use syntax and a performant construct for simple comparison inline.

YASS
$a = value(input("Please guess a number"))
$y = match($a :
  0 => "Incorrect";
  1 => "Incorrect";
  2 => "Incorrect";
  3 => "Incorrect";
  4 => "Correct";
)
print($y)
  

Whilst this does demonstrate one such use case, it is also lacking efficiency, so an else statement can be placed it as such:

YASS
$a = value(input("Please guess a number"))
$y = match($a :
  10 => "Correct";
  else => "Incorrect";
)
print($y)
  

Writing a guess the number game is also very easy using the match construct:

YASS
$rand = random_number(0, 10)
$result = ""
while ($result != "Correct")
  $user_input = value(input("Please guess a number"))
  $result = match($user_input : $rand => "Correct"; else => "Incorrect";)
  print($result)
end while
  

Syntactic sugar

YASS
$a = value(input("Please guess a number"))
$y = match($a :
  10 => "Correct";
  else => "Incorrect";
)
print($y)
  

PHP style

Since version 1.9.5 (Melrose, May 2021), match statements can be written using identical syntax to PHP 8's syntax. This means they can use the curly brace syntax. This example uses this coding style.

The following example generates a random number between 0 and 500 and assigns it to a variable. It doubles the value of the random number and stores that in a variable. The program then iterates between 0 and 1000 and when the value of the random number is it outputs "What I wanted".

The do_nothing function here means that the print command will not even print anything to the screen.

YASS
$rand = random_number(0, 500)
$double = $rand * 2
for($x = 0 to 1000)
  print(match($x) {
    $rand => "What I wanted";
    $double => "Double what I wanted";
    else => do_nothing();
  })
end for
  

Changes in ZPE 1.12.8+

ZPE 1.12.8 (Nessie Newt, August 2024) changed a considerable number of things about the match statement.

The first is that in the original version of the match statement commas were added as syntactic sugar to make it easier to read. As of ZPE 1.12.8 and YASS 24.7 they have be replaced with semi-colons and are now required.

Secondly, before ZPE 1.12.8, the else statement can be anywhere within the match statement. As of version 1.12.8 the else statement must go at the end.

Thirdly, the ZPE compiler no longer supports multiple else statements within a match statement as this was a waste of resources and the runtime would only interpret the first statement it finds.

Comments
Feedback 👍
Comments are sent via email to me.