StructOp Handbook
Contents Examples Commands
Handbook / Application language

Classes and interfaces

A class is a struct with methods: its fields work like any struct's, and the functions it lists act on one object of the class. An interface names methods that several classes share, so one function can work on any of them. Both come from StructOp/c and StructOp/rust 1.1.0 and build as C and as Rust from the same files.

On this page

Classes#

counter.st
ability "class" "Counter" {
    fields  = [{ name = "count", type = Int }, { name = "step", type = Int }]
    methods = [ability.function.counter_bump, ability.function.counter_total]
}

ability "function" "counter_bump" {
    body = [ability.set.Bump]
}

ability "set" "Bump" {
    target = self.count
    value  = self.count + self.step
}

ability "function" "counter_total" {
    returns = Int
    return  = self.count
}

ability "value" "c" {
    type    = Counter
    initial = { count = 0, step = 5 }
}

data "value" "c" {
    of = ability.value.c
}

Calling a method#

A method is called through its object, the value written before the method's name:

main.st
ability "call" "Bump" {
    function = ability.value.c.bump
}

ability "call" "Read" {
    function   = ability.value.c.total
    result     = ability.value.total
    depends_on = [ability.call.Bump]
}

args holds the other arguments, result the returned value, as for any call. Inside a method, function = self.bump calls a method of the same object. Calling the function directly (function = ability.function.counter_bump) is an error: a method always needs its object.

Interfaces#

shapes.st
ability "interface" "Shape" {
    methods = [{ name = "area", returns = Float },
               { name = "grow", params = [{ name = "by", type = Float }] }]
}

ability "class" "Square" {
    fields     = [{ name = "side", type = Float }]
    methods    = [ability.function.square_area, ability.function.square_grow]
    implements = [ability.interface.Shape]
}

ability "function" "report" {
    params = [{ name = "s", type = Shape }]
    locals = [{ name = "a", type = Float }]
    body   = [ability.call.Grow, ability.call.Area, ability.print.Show]
}

ability "call" "Grow" {
    function = s.grow
    args     = [1.0]
}

ability "call" "Area" {
    function = s.area
    result   = a
}

ability "call" "ReportSquare" {
    function = ability.function.report
    args     = [ability.value.sq]
}

What is generated#

In C, a method is a function whose first parameter is Counter *self; function = ability.value.c.bump becomes counter_bump(&st->c). An interface becomes a table of function pointers plus a value that carries the object's address and the table:

typedef struct {
    float (*area)(void *self);
    void (*grow)(void *self, float by);
} Shape_methods;
typedef struct { void *self; const Shape_methods *methods; } Shape;

static const Shape_methods Square_as_Shape = { Square_Shape_area, Square_Shape_grow };

report((Shape){ &st->sq, &Square_as_Shape });
a = s.methods->area(s.self);

In Rust, methods go into impl Counter { fn bump(&mut self) ... } and are called as c.bump(). An interface is a trait; the methods it names go into impl Shape for Square, and an interface parameter is &mut dyn Shape:

trait Shape {
    fn area(&mut self) -> f32;
    fn grow(&mut self, by: f32);
}

fn report(s: &mut dyn Shape) { /* ... */ }

report(&mut sq);
a = s.area();

structop graph draws a call through an object to the method's function, and a call through an interface parameter to the interface.

Errors#

MistakeMessage
a listed function without the class prefixfunction "area" is listed by class "Square", so its name must start with square_ (e.g. square_area)
a function listed by two classesfunction "square_area" is a method of class "Square" and class "Box"
a method reading a file-scope variablemethods of class "Square" use only self, their parameters and locals; var.n is a file-scope variable
a method using a file-level valuea method cannot use data.value.Best.current; pass it in as a parameter or update it at the call site
a method calling a function that uses onemethods of class "Counter" use only self, their parameters and locals; counter_bump calls log_total, which uses file-scope variables or values
a method the class does not haveclass "Counter" has no method "volume"
a method called as a plain functionfunction "counter_bump" is a method of class "Counter"; call it through an object, e.g. function = ability.value.<name>.bump
a method called on a variablevariables are read-only inputs; call methods on a value: function = ability.value.<name>.<method>
a variable passed to an interface parametervariables are read-only inputs; pass a value: args = [ability.value.<name>]
a data block passed to an interface parameterdata blocks are read-only; pass a value: args = [ability.value.<name>]
a method called on something that is not an objectability.value.total is not an object of a class, so it has no method "bump"
an interface method missing from the classclass "Square" implements "Shape" but has no method "grow"
different parameter or return typesmethod "grow" of class "Square" takes (Int); Shape.grow takes (Float)
an interface as a variable's typeShape can only be the type of a function parameter
an object whose class does not implement the interfaceclass "Circle" does not implement "Shape"
an interface method that does not existinterface "Shape" has no method "volume"

Scope#

Classes compose through interfaces rather than inheritance. An interface is used as the type of a parameter, and a method is called by a call ability. Classes and interfaces belong to the root configuration, like other types; modules declare objects of the root's classes and call their methods the same way.