Parameters

Parameters take on values that have been passed into a method, constructor, property method, or closure. (Note: the values passed in are called arguments).

There are three types of parameters in Plinth: normal, default, and auto-assign.

Default and auto-assign parameters both have special properties which mean some extra processing can be done before the function’s block executes. All parameters are processed just before execution of the function’s block, and in the order that they are written in the parameter list.

Normal Parameters

Normal parameters are the most commonly used ones, and are exactly the same as parameters in languages like Java and C♯. They take on a value from the corresponding argument, and act as a variable during the function they are declared in.

They can be declared as follows:

<type> <name>

Default Parameters

Default parameters are similar to normal parameters, except that they can only be declared after all of the normal and auto-assign parameters, and that the caller doesn’t have to pass them in as arguments.

Default parameters are declared as follows:

<type> <name> = <expression>

In Plinth, default parameters always have a name, and each default argument must give the name of the default parameter it fills in. If the caller doesn’t specify a value for a default parameter, a default expression is evaluated, and the result is assigned to the default parameter’s variable.

The default expressions are run just before the body of the function, in whichever order they are written.

If multiple default parameters are given, the default arguments can be given in any order, with an arbitrary set left out. If required, a default parameter can default to the value of another parameter that is written before it.

The default parameters are sorted by name before the order of parameters for the underlying function is generated, so it is binary compatible to change the order of the default parameters, but not to change their names.

Here is an example that shows how default parameters can be used:

void foo(uint x, string y = "", long z = getLong()) {
  // ...
}

// ... elsewhere ...
foo(12);
foo(12, z = 9, y = "hi");
foo(12, z = 15);

Auto-Assign Parameters

An auto-assign parameter called foo is basically a quick way of specifying a parameter called foo and then writing this.foo = foo; in the body of the function. It can be used anywhere a normal parameter can be used, and acts as a normal parameter from the caller’s perspective.

Auto-assign parameters are declared as follows:

@<name>

Where <name> is the name of a field or property. The type of the parameter is taken from the field or property it sets.

Auto-assign parameters do not work as variables, so if the name of an auto-assign parameter is referenced inside a function, it will refer to the field/property assigned to rather than the parameter itself (unless a local variable takes precedence).

Here is an example of how auto-assign parameters can be used:

class Person {
  property string name;
  uint age;

  create(@name, @age);

  void setAge(@age, string event) {
    stdout::println("Happy " + event + ", " + name + "!");
    age--;
    stdout::println("Oops, you're now " + this.age);
  }
}

// ... elsewhere ...

Person a = new Person("Alice", 123);
a.setAge(456, "birthday");
// Happy birthday, Alice!
// Oops, you're now 455

Note that the constructor does not have a block. This is fine, as the auto-assign parameters add an implicit block whenever they are used.

Table Of Contents

Previous topic

Exception Handling

Next topic

Statements

This Page