Declarations
Methods and functions
Declare callables with block or single-statement bodies, overloads, default parameters, named arguments and type parameters.
since 0.1.0-alpha.1linuxwindowswasm
Description
A function is a callable declared in a namespace or at the top level. A method is a callable
declared inside a class or struct; it has a this reference to the object it is called on. The
syntax is the same for both:
int f(int a, int b) { return a + b; } // block body
int g(int a, int b) => a + b; // arrow body: => means return
void say(string s) console.writeln(s); // any single statement is a body
void listen(int port = 80, string host = "localhost") { ... } // default parameters
U remap<U>(U v) => v; // type parameters on the callable
string s = remap::<string>("value"); // explicit type arguments
A body is exactly one statement: a block { ... } is one statement that holds many, an arrow
body => expr returns the expression, and any other single statement, such as a call, is allowed
without braces. Overloads are selected by the types of the arguments, for methods and functions
alike. Named arguments and default parameter values work the same way everywhere; see
lang.argument-binding for the exact binding rules.
Bodies, overloads, defaults, named arguments and generics
use io::console;
int add(int a, int b) { return a + b; }
int mul(int a, int b) => a * b;
void say(string s) console.writeln(s);
U remap<U>(U v) => v;
string describe(int n) => "int " + n.toString();
string describe(string s) => "string " + s;
string describe(float f) => "float " + f.toString();
void listen(int port = 80, string host = "localhost") {
console.writeln(host + ":" + port.toString());
}
class Counter {
int n = 0;
int bump() { n += 1; return n; }
T first<T>(Array<T> xs) => xs.at(0);
}
console.writeln(add(2, 3));
console.writeln(mul(2, 3));
say("hi");
string s = remap::<string>("value");
console.writeln(s);
console.writeln(remap(5));
console.writeln(describe(1));
console.writeln(describe("a"));
console.writeln(describe(1.5));
listen();
listen(8080);
listen(host: "example.org");
listen(host: "h", port: 1);
Counter c = Counter();
c.bump();
console.writeln(c.bump());
console.writeln(c.first(["x", "y"]));
console.writeln(c.first::<int>([7, 8]));
5
6
hi
value
5
int 1
string a
float 1.5e0
localhost:80
localhost:8080
example.org:80
h:1
2
x
7
Rules
- A body is one statement.
{ ... }groups several statements into one;=> expris shorthand for{ return expr; }; any other single statement is also a body. - A callable whose result is not
voidmust return a value on every path (lang.statements): a body whose end is reachable, or a barereturn;in it, is a compile error. A path that throws or loops forever needs noreturn. - A method has
this; a function does not. Usingthisoutside a class is a compile error. - A declaration with
;in place of a body states a requirement in an interface, which implementing classes must fulfil. In a class or struct, a method, operator,getorsetdeclared with;has an empty body, the same as{}: avoidmethod or a setter declared that way does nothing when called, and one with a result type is a compile error at the declaration, because it does not return a value (the rule above). That holds for a member a subclass is meant to override too. A data field that a subclass declares with the name of a;setter is written directly instead. - A function declared with
;at the top level or in a namespace, and defined with a body elsewhere in the same scope with the same name and signature, is a forward declaration: both name one function, and every call runs the definition. It is never needed, because a function is visible before its definition. Default values, attributes and modifiers such asprivatebelong on the definition; any of them on the forward declaration is a compile error. Named arguments use the definition's parameter names. - Overloads share a name and differ in their parameter types or count. The overload is chosen by
the argument types (
lang.argument-binding). - A callable may declare type parameters after its name:
U remap<U>(U v). They are inferred from the arguments when possible. - An explicit type-argument list is written
name::<T>(args)for a function andobject.name::<T>(args)for a method. It is checked first and must supply every type parameter; named, default and injected arguments are then bound as for any other call. - A parameter may carry a default value (
int port = 80); the default must be a compile-time constant that does not refer to another parameter or tothis. - Arguments may be named (
listen(host: "h")). Positional arguments come first.
Examples
Several statements in a body need braces; without them the following statement is not part of the body:
One statement per body
use io::console;
void g() console.writeln("a"); console.writeln("b");
g();
b
a
console.writeln("b") is a separate top-level statement here, so it runs first; g only prints a.
A void method or a setter declared with ; in a class has an empty body; calling it does
nothing, and a subclass may override it:
An empty body declared with ;
use io::console;
class Shape { void draw(); set scale(int k); }
class Square : Shape { void draw() { console.writeln("square"); } }
Shape s = Shape();
s.draw();
s.scale = 2;
console.writeln("nothing drawn");
Shape q = Square();
q.draw();
nothing drawn
square
A member with a result type declared with ; in a class, such as string name();, is a compile
error at the declaration (method 'name' does not return a value on every path, expected 'string'); in an interface the same line is a requirement.
See also
- Operator declarations — Define operators on a class or struct by declaring a member with a symbolic selector such as (+), (==) or ([]).
- Argument binding — How a call's arguments are matched to parameters, filled by defaults or injection, and how overloads are ranked.
- Constructors — Declare constructors with new, select among them by label and argument types, call base constructors, and construct classes that declare none.