Fork me on GitHub

Variable and Field Declaration

Variables and fields can be declared in SARL. To declare a variable or a field, you must specify if it is a value or a variable (see below for details), its name, and optionally its type and its initial value.

The variable/value declaration follows the syntax:

	var NAME [: TYPE] [= INITIAL VALUE]
	val NAME [: TYPE] [= INITIAL VALUE]

Shadowing variables from outer scopes is not allowed, the only exception is the implicit variable it.

##1. Variable vs. Value Declaration

A variable declaration starting with the keyword val denotes a value, which is essentially a final, unsettable variable.

The variable needs to be declared with the keyword var, which stands for ‘variable’, if its value can change.

Variables declared outside a lambda expression using the var or val keyword are accessible from within the lambda expressions.

Fields declared outside a lambda expression using the var keyword or the val keyword are accessible from within the lambda expressions.

##2. Typing

The type of the variable itself can either be explicitly declared or it can be inferred from the initializer expression.

In the following example, the type of the variable is explicitly given:

var a : String = "abc"

In the following example, the type of the variable is inferred to String:

var a = "abc"

##3. Implicit Variables: this and it

Like in Java the current object is bound to the keyword this. This allows for implicit field access or method invocations.

You can use the variable name it to get the same behavior for any variable or parameter. Moreover, the variable it is allowed to be shadowed. This is especially useful when used together with lambda expressions.

When you type a name that doesn’t resolve to a variable in the local scope, the compiler tries to find a field with the same name on the it object, then in the this object.

##4. Acknowledgements

This documentation is inspired by the documentations from the Xtext and Xtend projects.

##5. Legal Notice

Copyright © 2014-2019 the original authors or authors.

Licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the License. You may obtain a copy of the License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator io.sarl.maven.docs.generator 0.10.0.