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#
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
}methodslists ordinary functions. Each name starts with the class name and an underscore (counter_bumpforCounter, case ignored); the rest is the method's name (bump).- Inside a method,
selfis the object. It is not listed inparams;self.countreads a field andsetwithtarget = self.countwrites it. - A function belongs to at most one class.
- Methods use only
self, their parameters and their locals, never file-scope state: notvar.total,param.step, module outputs, a file-levelvalueability (ability.value.Best) or adatablock that reads one. They call no function that uses one. A method returns its result and the caller combines it with the file-level value. - Objects are values (
ability "value"); they, locals and fields behave like struct values: defaults, field access, whole-objectset.
Calling a method#
A method is called through its object, the value written before the method's name:
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#
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]
}- Each interface method has a
name, optionalparamsand an optionalreturns. implementspromises that the class has every method of the interface, with the same parameter types in the same order and the same return type.validatechecks it.- An interface is only the type of a function parameter. Such a parameter takes an object of any class that implements the interface (
args = [ability.value.sq]),selfinside a method, or another parameter of that interface. The methods may change the object, so a variable or a data block is refused: pass the value itself. function = s.areacalls the method of whichever class the object belongs to.
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#
| Mistake | Message |
|---|---|
| a listed function without the class prefix | function "area" is listed by class "Square", so its name must start with square_ (e.g. square_area) |
| a function listed by two classes | function "square_area" is a method of class "Square" and class "Box" |
| a method reading a file-scope variable | methods of class "Square" use only self, their parameters and locals; var.n is a file-scope variable |
| a method using a file-level value | a 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 one | methods 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 have | class "Counter" has no method "volume" |
| a method called as a plain function | function "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 variable | variables are read-only inputs; call methods on a value: function = ability.value.<name>.<method> |
| a variable passed to an interface parameter | variables are read-only inputs; pass a value: args = [ability.value.<name>] |
| a data block passed to an interface parameter | data blocks are read-only; pass a value: args = [ability.value.<name>] |
| a method called on something that is not an object | ability.value.total is not an object of a class, so it has no method "bump" |
| an interface method missing from the class | class "Square" implements "Shape" but has no method "grow" |
| different parameter or return types | method "grow" of class "Square" takes (Int); Shape.grow takes (Float) |
| an interface as a variable's type | Shape can only be the type of a function parameter |
| an object whose class does not implement the interface | class "Circle" does not implement "Shape" |
| an interface method that does not exist | interface "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.