Skip to content

Objects, Functions, and Object Generators

Socratic_Phoenix edited this page Aug 19, 2017 · 3 revisions

<-- Back | Next -->


Objects

To create an object, open a scope block preceded by #, for example:

# {

}

creates an empty object. The code within the scope block will be run on the object's scope, and it can then be used as an object. For example:

# {
    value = 5
    etc = 12
}

will create an object with the fields value and etc. You can also set fields to functions (see function syntax below). Note that all scripts/modules are 'top level' objects.

Functions

Functions are specialized objects, you can create a function by opening a scope block preceded by $ and parameters, for example:

$ () {
    
}

creates a no-op function. The following function adds two values:

$ (left,right) {
    return: left + right
}

But, you may be thinking, functions are objects, so how to I apply object properties to them? Simple, use the static keyword:

$ (left,right) {
    static value = 0
    return: left + right + value
}

The above creates a function which, as an object, has the field value, and as a function returns left + right + value. The static keyword may precede any instruction in a function block to move the instruction to object initialization. Finally, you can set default values for parameters:

$ (left,right=0) {
    return: left + right
}

All default parameters must come after all required parameters.

Object Generators

Object generators are syntactic sugar for functions that return objects, and they are as close to classes as Shnap gets. You can create an object generator by opening a scope block preceded by @ and parameters, for example:

@ () {

}

Is a function generator that returns an empty object. The syntax for an object generator is precisely the same as an object, except for two key differences:

  • It can have parameters like a function
  • It has the static keyword like a function For example, a simple range implementation might look like so:
range = @ (min,max,step=void) {
    static defStep = 1

    if (step == void) step = defStep

    hasNext = $ () {
        return: min < max
    }    

    next = $ () {
        ret = min
        min += step
        return: ret
    }

}

Omissions

Similar to blocks, many parts of an object, function, or object generator can be omitted in different circumstances. First, as with blocks, the braces may be omitted if the block has only one instruction, for example, all of the following are valid:

# onlyVal = 5
$ () return: 4
@ () return: 4

Also similarly to blocks, the parenthesis for parameters may be omitted:

$ arg1,arg2 return: 4
@ return: 4

There are, however, some cases where these parenthesis are required:

@ val = 5 //The parser assumes that val is a parameter, this can be fixed in two ways (I personally prefer the second)
@ () val = 5
@ {val = 5}

Because of this, simple lambda functions may be written like so:

$ (arg) return: arg + 1
Clone this wiki locally