This section describes the syntax for using or calling the members of an object.
A simple name can refer to a field, variable, or parameter. In addition, it can point to a method with zero argument since empty parentheses are optional.
The rest of this section describes particular mechanisms for calling the object members.
The SARL language provides a very powerfull mecanism for calling members of an object as properties of this object.
If there is no field with the given name and also no method with
the name and zero parameters accessible, a simple name binds to a
corresponding Java-Bean getter method if available.
The getter method must have a name starting with one of the strings of
characters get
, is
, has
, followed by the given name.
In the following example, two fields are defined: prop1
and prop2
.
As usual, these properties have a private scope, and the getter and setter functions must
be defined to enable public scope access.
In the example, only the getter and setter functions for prop2
are defined.
agent A {
var prop1 : Object
var prop2 : Object
def getProperty2 : Object {
return this.prop2
}
def setProperty2(o : Object) {
this.prop2 = o
}
}
Four cases for accessing a property are possible:
Getter Function Def. | Access Type | Example | Effect |
---|---|---|---|
A getter is defined | Functional | this.getProperty2 |
Invocation of the getter function |
A getter is defined | Property-access | this.property2 |
Invocation of the getter function |
A getter is defined | Field-access | this.prop2 |
Read the field if it is visible |
A getter is not defined | Field-access | this.prop2 |
Read the field if it is visible |
The two first cases have the same effect: the getter function is called. But, the second case uses the “property-access” syntax, and it is assumed to be easier to be read in the code.
Four cases for writing a property are possible:
Setter Function Def. | Access Type | Example | Effect |
---|---|---|---|
A setter is defined | Functional | this.setProperty2(x) |
Invocation of the setter function |
A setter is defined | Property-access | this.property2 = x |
Invocation of the setter function |
A setter is defined | Field-access | this.prop2 = x |
Write the field if it is visible |
A setter is not defined | Field-access | this.prop2 = x |
Write the field if it is visible |
The two first cases have the same effect: the setter function is called. But, the second case uses the “property-access” syntax, and it is assumed to be easier to be read in the code. Note that for this latest case, the “variable assignment” syntax is used.
To access a static field or method you can use the recommended Java syntax or the more explicit double colon ::
.
That means, the following expressions are pairwise equivalent:
var a = Integer::TYPE
var b = Integer.TYPE
Checking for null references can make code very unreadable.
The variable myRef
is assumed to be declared.
if (myRef !== null) myRef.length() else 0
In many situations, it is correct for an expression to return a default value (null
, 0
…) if a receiver was null
.
SARL includes the safe navigation operator ?.
. to do the null-check test and make such code better readable.
myRef?.length()
When it is possible to extend an existing type, the methods can be overridden.
In this case, the super
keyword invoks the inherited implementation of the method
from the overriding method.
def anAction {
// Call the inherited implementation
super.anAction
}
Constructor calls correspond to the calls of a constructor function for an object.
Constructor calls have the same syntax as in Java. The only difference is that empty parentheses are optional. If type arguments are omitted, they will be inferred from the current context similar to Java’s diamond operator on generic method and constructor call.
var a = new Integer(345)
var b = new ArrayList<Integer>()
var c = new ArrayList<Integer>
In the implementation of a constructor, it is possible to call one of the inherited constructors. The syntax is similar to Java: the (:super:] keyword is used to represent the inherited constructor.
Important Note We recommend that you include the parentheses when invoking the default constructor of the super type because, in some cases, typing super
alone (without the parenthesis pair) has no effect, and that is an error.
new {
super() // Call the inherited default constructor
}
new (param : Address) {
super(param) // Call the inherited constructor with a parameter
}
This documentation is inspired by the documentations from the Xtext and Xtend projects.
Copyright © 2014-2024 SARL.io, the Original Authors and Main Authors.
Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.
Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.
You are free to reproduce the content of this page on copyleft websites such as Wikipedia.
Generated with the translator docs.generator 0.14.0.