Access modifiers limit access to certain classes, variables, and functions; the basic access modifiers are as follows:
public allows everything to access it.
protected limits access to the same package (folder), including sub-packages (default for non-inner classes).
private prevents anything else from accessing it except things that are declared in the same scope (default unless in a non-private scope or a class in the global scope).
An example usage is as follows:
public class Foo {
private x : Int = 1;
protected y : Int = 1;
}
Access modifiers can be applied to functions, variables/constants, classes, getters, setters, etc.
Access Blocks
Because adding access modifiers to everything can be a bit verbose, Silicon takes a page from C++'s book and allows you to declare access blocks: access modifiers followed by curly brackets that applies that modifier to everything within. For example, the following variables are all private:
class Foo {
private {
var x : Int = 1;
var y : Int = 1;
var z : Int = 1;
}
}
More Controlled Access
While the main three access modifiers do allow you to control access, they aren't very flexible. In response to this, Silicon introduces a keyword for better flexibility exposeto. This keyword is followed by every type you wish to share these with (inlcuding primitives and generics that are within scope). For example, the following variables are accessible by Foo and Bar only.
exposeto Foo, Bar {
var x : Int = 1;
var y: Int = 1;
}
Do note that exposeto can be used in the non-block form, but this is not recommended due to verbosity.
Overview
Access modifiers limit access to certain classes, variables, and functions; the basic access modifiers are as follows:
public
allows everything to access it.protected
limits access to the same package (folder), including sub-packages (default for non-inner classes).private
prevents anything else from accessing it except things that are declared in the same scope (default unless in a non-private scope or a class in the global scope).An example usage is as follows:
Access modifiers can be applied to functions, variables/constants, classes, getters, setters, etc.
Access Blocks
Because adding access modifiers to everything can be a bit verbose, Silicon takes a page from C++'s book and allows you to declare access blocks: access modifiers followed by curly brackets that applies that modifier to everything within. For example, the following variables are all private:
More Controlled Access
While the main three access modifiers do allow you to control access, they aren't very flexible. In response to this, Silicon introduces a keyword for better flexibility
exposeto
. This keyword is followed by every type you wish to share these with (inlcuding primitives and generics that are within scope). For example, the following variables are accessible by Foo and Bar only.Do note that
exposeto
can be used in the non-block form, but this is not recommended due to verbosity.