Standard Functions and Self

Parent Previous Next

Standard Functions and Self.

SCRAMBLECODE has a library of built-in standard functions related to every type.


They are written in an x.y format as type-dot-function. They are recognized by the compiler as "member-functions" of the type - even if the type is just a simple integer or string type.


 Function Foo()

 {

   String  s := "Hello";

   Integer i := s.Length(); //Now i == 5;

 }


All standard functions are described in detail later on, also the String.Length used in the previous example.


If a standard function returns a value of some type, then this value also has standard functions available. An expression can be built like a chain of standard functions:


 Function Foo()

 {

   String s := (123456).ToString().Copy(2, 3); //Now s == "345";

 }


However, not all standard functions return a value. Instead they execute the functional operation on themselves. An example of this is the Blob.Clear.



Return Self.

Some standard functions have the additional feature that they return themselves as the result of an executed operation. We refer to this as returning Self which is also sometimes written as < Self >.


The Integer type for instance has a number of standard functions which operate on themselves and returns Self. Two of these are the functions Integer.ParseString and Integer.Inc, which are used in this example:


 Function Foo()

 {

   Integer i.ParseString("99").Inc().Inc(); //Now i == 101;

 }


In order to use a chain of functions working on Self, all previous functions must return Self and must refer to a specific variable. The next example is not acceptable because the Integer.Abs function does not return Self even though it does return an integer value:


 Function Foo()

 {

   Integer i.ParseString("-99").Abs().Inc().Inc(); //Not accepted by compiler

 }