spool-lang / Spool-Legacy-Repo

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

Variables, Constants, Getters and Setters #3

Open RedstoneParadox opened 5 years ago

RedstoneParadox commented 5 years ago

Variables and Constants

Variables and Constants are values that are mutable and immutable, respectively. Variables are declared with the var keyword while constants are declared with the const keyword, followed by the variable name

var x = 3
const y = 7

If a Variable or Constant is not initialized right away, you would mark it with the latient keyword and specify its type using Pascal Notation:

latient var x : Int
latient const y : Int

Getters and Setters

All Variables and Constants implicitly have getter and setter functions, but you can define your own using the get and set keywords to denote getter and setter functions. Getters and setters have the same name as their respective variable; can have a different access modifier than their variable; do not have a declared return type, and use the value keyword to refer to their value. Getters also have a single parameter and do not need to specify the type of the parameter.

var x = 3

get x() {
    return value
}
private set x(input) {
    if (input > value) {
        value = input
    }
}

The getter or setter is called when the assignment operator = is used, depending on wether you are getting the value or setting it.

Bounds

You can also specify the bounds of a variable if its type overloads the range (...) operator using the bound keyword.


var percent : Byte bound 1...100 = 3

This is a bit of syntactic sugar that helps avoid checking for this sort of thing using an if-statement in a setter, thus allowing for more concise code. It is also something that an IDE can pick up on, helping reduce errors in large codebases.

RedstoneParadox commented 5 years ago

(Version 1)

Variables and Constants are values that are mutable and immutable, respectively. Variables are declared with the var keyword while constants are declared with the const keyword, followed by the variable name

var x = 3;
const y = 7;

If a Variable or Constant is not initialized right away, you would mark it with the latient keyword and specify its type using Pascal Notation:

latient var x : Int
latient const y : Int

All Variables and Constants implicitly have getter and setter functions, but you can define your own using the get and set keywords to denote getter and setter functions. Getters and setters have the same name as their respective variable; can have a different access modifier than their variable; do not have a declared return type, and use the value keyword to refer to their value. Getters also have a single parameter and do not need to specify the type of the parameter.

var x = 3;

get x() {
    return value
}
private set x(input) {
    if (input > value) {
        value = input
    }
}

The getter or setter is called when the assignment operator = is used, depending on wether you are getting the value or setting it.