spool-lang / Spool-Legacy-Repo

Legacy repo for the Spool programming language.
3 stars 0 forks source link

Access Modifiers #10

Open RedstoneParadox opened 5 years ago

RedstoneParadox commented 5 years ago

Overview

Access modifiers limit access to certain classes, variables, and functions; the basic access modifiers are as follows:

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.