Skip to content

Declaring a function

Every function in Novis is a method of a class. A function that does not need an object is a static method. You call it with the class name: Price::show(1000). There are no functions outside a class. The built-in functions are methods too, such as Core\Str::length($s).

Every parameter and the return type are written out. There is no default type.

void, never, self and static

Four return types say something an ordinary type cannot.

void means the method returns no value. You call it as a statement of its own, and you cannot use the result.

never means the method does not return to its caller. It always throws an error or ends the program.

self and static both name a class. self is the class the method is written in. static is the class the call names. So a method in a base class can return an instance of the child class it was called on, and the caller gets that child class. $o is static checks whether $o is an object of that class.

The examples show a method called only for what it does, a chain of calls on one object, and a base class that creates an instance of the child class the call names. Example 04 shows that a void result cannot be printed. Example 05 shows that a never call ends the caller's path. Example 06 shows a parameter of type callable(static). Example 07 shows a finder that returns only objects of the class the call names.

A method called only for what it does

<?nvs
// A method that returns no value has the return type `void`.
// Each call below is a statement of its own.
class Audit
{
public static function note(string $what, uint $count): void
{
echo '[audit] ', $what, ' x', $count, "\n";
}
public static function line(string $text): void
{
echo $text, "\n";
}
}
Audit::line('import starting');
Audit::note('rows read', 120);
Audit::note('rows skipped', 3);
Audit::line('import finished');
Output
import starting
[audit] rows read x120
[audit] rows skipped x3
import finished

A chain of calls that each hand the object back

<?nvs
// Each method returns `self`, the class it is written in, and returns `$this`.
// So you can call the next method on the result, all on one line.
final class Query
{
public string $table = '';
public array<string> $wheres = [];
public uint $limit = 0;
public function from(string $table): self
{
$this->table = $table;
return $this;
}
public function where(string $clause): self
{
$this->wheres[] = $clause;
return $this;
}
public function take(uint $rows): self
{
$this->limit = $rows;
return $this;
}
public function text(): string
{
return 'select from '
. $this->table
. ' where '
. Core\Str::join($this->wheres, ' and ')
. ' limit '
. $this->limit;
}
}
Query $q = new Query();
echo $q->from('orders')->where('paid')->where('shipped')->take(25)->text(), "\n";
Output
select from orders where paid and shipped limit 25

One base class that builds whichever kind was asked for

<?nvs
// The base class `Record` has one `load` method for every subclass.
// `load` returns `static`, the class named in the call. So `Invoice::load` returns an `Invoice`.
// `refuse` returns `never`, because it always throws an error.
class Record
{
public function constructor(public uint $id)
{}
public static function load(uint $id): static
{
if ($id == 0) {
Record::refuse('a record id is never zero');
}
return new static($id);
}
public static function refuse(string $why): never
{
throw new RuntimeError($why);
}
public function label(): string
{
return 'record #' . $this->id;
}
}
final class Invoice extends Record
{
public function label(): string
{
return 'invoice #' . $this->id;
}
}
final class Customer extends Record
{
public function label(): string
{
return 'customer #' . $this->id;
}
}
Invoice $invoice = Invoice::load(1042);
Customer $customer = Customer::load(7);
echo $invoice->label(), "\n";
echo $customer->label(), "\n";
try {
Invoice::load(0);
} catch (Throwable $e) {
echo "refused an impossible id\n";
}
Output
invoice #1042
customer #7
refused an impossible id

A void result cannot be printed

<?nvs
// Shows that the result of a `void` method cannot be printed.
// This program does not compile.
// proof: compile-error
class Helper
{
public static function nothing(): void
{}
}
echo Helper::nothing();
Output
error[E0707]: `void` has no string form
--> 04-a-void-result-cannot-be-printed.nvs:12:6
|
12 | echo Helper::nothing();
| ^^^^^^^^^^^^^^^^^ converted to `string` here
= help: a call that returns `void` has no value at all, so there is nothing here to print
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

A never call ends the path

<?nvs
// `first` needs no `return` after `Report::fail`, because `fail` returns `never`.
class Report
{
public static function fail(string $why): never
{
throw new RuntimeError($why);
}
public static function first(array<int> $rows): int
{
if (Core\Arr::count($rows) > 0) {
return $rows[0];
}
Report::fail('no rows');
}
}
echo Report::first([3, 4]), "\n";
try {
Report::first([]);
} catch (RuntimeError $e) {
echo 'error: ', $e->message, "\n";
}
Output
3
error: no rows

A check for the class the call names

<?nvs
// In a parameter type, `static` is the called class: the class the call names.
// So `Order::passes` takes a function for `Order`.
class Model
{
public function constructor(public string $id)
{}
// `callable(static): bool` is a function that takes an object of the called class.
public static function passes(string $id, callable(static): bool $rule): bool
{
// `new static($id)` is an object of the called class, so this call compiles.
return $rule(new static($id));
}
public function describe(callable(static): string $format): string
{
// At a call on an object, the called class is the class of `$this`.
return $format($this);
}
}
final class Order extends Model
{
public int $total = 120;
}
// The function takes an `Order`, so it can read `$o->total`.
echo Order::passes('o-1', fn(Order $o): bool => $o->total > 100) ? "big order\n" : "small order\n";
echo Order::passes('o-2', fn(Order $o): bool => $o->total > 500) ? "big order\n" : "small order\n";
Order $order = new Order('o-3');
echo $order->describe(fn(Order $o): string => $o->id . ' costs ' . $o->total), "\n";
Output
big order
small order
o-3 costs 120

A finder that returns the class the call names

<?nvs
// One base class keeps every loaded object, and `Order::findAll()` returns only the orders.
class Model
{
private static array<Model> $loaded = [];
public function constructor(public string $id)
{}
// `new static($id)` creates an object of the class the call names.
public static function load(string $id): void
{
Model::$loaded[] = new static($id);
}
public static function findFirst(): ?static
{
foreach (Model::$loaded as Model $object) {
// `is static` checks if `$object` is of the class the call names.
// Inside this block, `$object` may be returned as `?static`.
if ($object is static) {
return $object;
}
}
return null;
}
public static function findAll(): array<static>
{
// `$found` only accepts objects of the called class, so it may be returned as `array<static>`.
array<static> $found = [];
foreach (Model::$loaded as Model $object) {
if ($object is static) {
$found[] = $object;
}
}
return $found;
}
}
final class Order extends Model
{
public function label(): string
{
return 'order ' . $this->id;
}
}
final class Customer extends Model
{}
Customer::load('c-1');
Order::load('o-1');
Customer::load('c-2');
Order::load('o-2');
// `Order::findAll()` returns `array<Order>`, so the loop can call `label`.
// Only `Order` has that method.
foreach (Order::findAll() as Order $order) {
echo $order->label(), "\n";
}
// `Customer::findFirst()` returns the first customer, so this prints `c-1`.
echo Customer::findFirst()?->id ?? 'none', "\n";
echo Core\Arr::count(Model::findAll()), " objects in total\n";
Output
order o-1
order o-2
c-1
4 objects in total

Details

void and never are allowed only as a return type. A parameter or a variable of type void or never is the error E0742. var $x = Helper::nothing(); is E0742 too, because the call returns void.

A void result is not a value. echo Helper::nothing(); is the error E0707. Passing it to an int parameter is E0401. A void method that returns a value is E0403.

A never method must not reach the end of its body. That is the error E0739. A return; in it is E0822. Every path must throw, call exit or call another never method.

A call to a never method ends the caller's path. The caller needs no return after it. This is true for a static method and for a method no child class overrides. A call to an overridden method does not end the path, so the caller still needs a return there. Otherwise it is E0739.

self and static are only allowed inside a class. Outside one, self is E0303. self also works as a parameter type and inside another type, such as array<self> or ?static. static is not allowed as a parameter type.

static is allowed inside a callable parameter type, such as callable(static): bool. Then Order::check(...) takes a function for Order. Inside the method, $rule($this) and $rule(new static()) are checked when the program compiles. Any other argument to $rule is the error E1015. If the method stores $rule or passes it to another function, that copy is a callable with no parameter types. Its arguments are checked when it runs.

A method that returns static must return $this or new static(...). return new self(); is E0741, because a child class may be the class the call names.

$o is static checks the class the call names when the program runs. In Model, called as Order::findAll(), it is true for an Order and false for a Customer. Inside if ($o is static), the method may return $o as static. After a write to $o, it may not. static inside a larger tested type, such as $xs is array<static>, is the error E1021. Test each element with is static instead. Inside an anonymous function, is static is the error E0834.

A variable may also have static in its type, such as array<static> $found = [];. The method may return it. Each value written to it must be $this, new static(...), null, a variable inside if ($x is static), or a call that returns static. Any other value is the error E1022. Passing the variable to an inout parameter is E1022 too, because that method may write any object to it. In a final class, static is that class, so these variables accept any object of it.

self and static are not values. return self; is the error E0321. To use a member of the class, write self:: and the member's name.

  • Do put related functions in one class, such as Price::withShipping and Price::show.
  • Do use static as the return type of a method that creates an object, so a child class gets its own type.
  • Don’t use the result of a void method. It has no value.
  • Don’t write function outside a class. It does not compile.