1. Methods and types
In Novis, every function is a method of a class. You write the type of each parameter and the type
the method returns. Save this program as tour.nvs and run it with nvs run tour.nvs:
<?nvs// Every function is a method of a class.class Greeter{ // A method says the type of each parameter and the type it returns. public static function greeting(string $name, int $hour): string { if ($hour < 12) { return 'Good morning, ' . $name . '!'; } return 'Good afternoon, ' . $name . '!'; }}
echo Greeter::greeting('Ada', 9), "\n";echo Greeter::greeting('Linus', 15), "\n";Good morning, Ada!Good afternoon, Linus!public static function declares a method that you call on the class, with Greeter::greeting.
. joins two strings.
The compiler checks every call against the types. Change 9 to "nine" and run the program
again. It does not run, and nvs prints this error:
error[E0401]: expected `int`, found `string`The error is found before the program starts. A web application with this mistake does not start either, so no request ever reaches the broken code.
Read more in Syntax: declaring a function and Syntax: declaring a class.
Next: step 2, arrays and loops.
