20 Interfaces
A function that adds up areas should work for circles, for squares, and for the shape somebody writes next year. A function that sends a message should work through a serial port, a radio, or a log kept in memory for testing. An interface says what all of them can do, and each class says that it is one.
20.1 Declaring an interface
An interface is a list of functions with no bodies: what can be asked, not how it is done.
interface Shape {
function area(self) -> float
function scale(self, by: float)
}
class Circle is Shape {
radius: float
function area(self) -> float {
return 3.14159 * self.radius * self.radius
}
function scale(self, by: float) {
self.radius = self.radius * by
}
}
class Square is Shape {
side: float
function area(self) -> float {
return self.side * self.side
}
function scale(self, by: float) {
self.side = self.side * by
}
}
function total(shapes: list[Shape]) -> float {
var sum = 0.0
for shape in shapes {
sum += shape.area()
}
return sum
}
let shapes: list[Shape] = [Circle(1.0), Square(2.0)]
print(total(shapes))
for shape in shapes {
shape.scale(2.0)
}
print(total(shapes), shapes)7.14159
28.56636 [Circle(radius=2.0), Square(side=4.0)]
total was written once. shape.area() runs
the area of whatever class the object is, and that is
decided while the program runs. An object seen through an interface
still prints as the class it is.
20.2 The rules
An interface lists functions,
each with self first and a type on every other parameter
and on the result. It has no fields and no bodies.
A class says which interfaces it is,
after is on its first line:
class Box is Named, Sized. It is not one unless it says
so.
The class has every function of the interface, written the same way
: the same parameter names, the same types, the same result. It may have more of its own. A missing function, or one written differently, is reported at the class:
interface Shape {
function area(self) -> float
function scale(self, by: float)
}
class Dot is Shape {
function area(self) -> float {
return 0.0
}
}error: `Dot` says it is a `Shape`, and a `Shape` has `function scale(self, by: float)`. Give `Dot` that function
--> main.zeno:6:1
|
6 | class Dot is Shape {
| ^
1 error found. Nothing was run.
interface Shape {
function scale(self, by: float)
}
class Dot is Shape {
function scale(self, factor: float) {
}
}error: `scale` of `Dot` is `function scale(self, factor: float)`, and `Shape` asks for `function scale(self, by: float)`. Write the two the same
--> main.zeno:5:1
|
5 | class Dot is Shape {
| ^
1 error found. Nothing was run.
In Go, and in Python’s protocols, any class that happens to have an
area and a scale is a Shape
without a word. That saves a line and costs two things: a class whose
area means something else is a Shape by
accident, and a class meant to be one, with scale misspelt,
is found out far away, at some call that passes it. With
is Shape the promise is written where the reader of the
class sees it, and it is checked there. (Decision 10.4.)
20.3 Where an interface goes
An object of a class goes wherever one of its interfaces is wanted: a
variable, a parameter, a result, a field, and each of those with
or none.
A list of the class does not go where a list of the interface is
wanted, because whoever held it as a list[Shape] could then
append a Square to a list of circles:
interface Shape {
function area(self) -> float
}
class Circle is Shape {
radius: float
function area(self) -> float {
return 3.0 * self.radius * self.radius
}
}
function total(shapes: list[Shape]) -> float {
var sum = 0.0
for shape in shapes {
sum += shape.area()
}
return sum
}
let circles = [Circle(1.0), Circle(2.0)]
print(total(circles))error: `shapes` of `total` should be list[Shape], but this is list[Circle]. Whoever holds it as a list of Shape could append another class to it. Say what the list holds where it is made, as in `let items: list[Shape] = [...]`
--> main.zeno:18:13
|
18 | print(total(circles))
| ^
1 error found. Nothing was run.
The message says what to do: say what the list holds where it is
made, by writing list[Shape] as the type of
circles. A list written out in place, as in
total([Circle(1.0)]), is a list of whatever the place
wants. (Decision 10.7.)
20.4 Through an interface, only its functions
Through an interface, only the interface’s functions can be used. Not the fields, not the class’s other functions, and there is no way back from the interface to the class:
interface Shape {
function area(self) -> float
}
class Circle is Shape {
radius: float
function area(self) -> float {
return 3.0 * self.radius * self.radius
}
}
let s: Shape = Circle(1.0)
print(s.radius)error: `radius` is not a function of the interface `Shape`, which has `area`. What else this object has depends on its class, and that is only known when the program runs. Give `Shape` a function for it
--> main.zeno:11:7
|
11 | print(s.radius)
| ^
1 error found. Nothing was run.
What else an object has depends on its class, and that is only known when the program runs. If code needs it, the interface should have a function for it.
A way back, such as Java’s casts or Rust’s downcast,
lets code ask "which class are you really?" and do something different
for each, which is what the interface was meant to save. When the
classes are a fixed list known in advance, an enum and
match say that directly. (Decision 10.8.)
20.5 Errors through an interface
An interface function can say what it raises. A class may raise less than the interface says, never more, because whoever calls it through the interface would not know:
class Full {
waiting: int
}
interface Port {
function send(self, text: str) -> int raises Full
function name(self) -> str
}
class Uart is Port {
speed: int
queued: int = 0
function send(self, text: str) -> int raises Full {
if self.queued >= 2 {
raise Full(self.queued)
}
self.queued += 1
return len(text)
}
function name(self) -> str {
return f"uart at {self.speed}"
}
}
class MemoryLog is Port {
lines: list[str] = []
function send(self, text: str) -> int {
self.lines.append(text)
return len(text)
}
function name(self) -> str {
return "memory log"
}
}
function report(port: Port, text: str) {
try {
print(port.name(), "sent", port.send(text))
} except Full as f {
print(port.name(), "is full with", f.waiting)
}
}
let ports: list[Port] = [Uart(9600), MemoryLog()]
for turn in range(3) {
for port in ports {
report(port, "hello")
}
}uart at 9600 sent 5
memory log sent 5
uart at 9600 sent 5
memory log sent 5
uart at 9600 is full with 2
memory log sent 5
MemoryLog never fills up and raises nothing, and that is
fine. A class whose send raised something Port
does not list would be refused at the class.
20.6 No parent classes
There is no inheritance in Zeno: a class cannot be another class, only interfaces. What two classes share is declared as an interface that both are (Classes).
Inheriting fields and code brings the questions every language with
it has had to answer: which area runs, what the object
looks like in memory, what happens when two parents have the same field.
None of them is needed to write total. (Decision 10.4.)
20.7 What it costs
In a compiled program, each object carries a small number saying
which class it is, and a call through an interface is a
switch on that number to the class’s function. There are no
tables of function pointers, and nothing is looked up by name. (Decision
10.9.)
An interface lists functions with no bodies. A class says which
interfaces it is with class Name is Interface, and must
have each function written the same way. An object goes where its
interface is wanted, a list of it only where the list was made as one.
Through an interface only its functions are there. A class may raise
less than its interface says, never more.
Add a
Triangleto the shapes, and check thattotalneeds no change.Write
interface Sensorwithfunction read(self) -> floatand two classes: a fake sensor that gives values from a list, and one that always gives the same value. Write a function that averages ten readings from any sensor.Give
Shapea functionname(self) -> strand make every class say what it is.Make
MemoryLog.sendraise aValueErrorfor an empty text, and read the message.