Why modularity
Spaghetti code works, and it works fast, right up to the day somebody has to change it. Experience taught me the difference. A system that is properly architected is easier to extend, and easier to refactor when other people change it later, and most of a large system's life is spent being changed. Of all the rules that make an architecture proper, I count modularity as the king: dividing a program into parts, each with a job and a boundary, so that a change stays inside the part it concerns.
On this page I will not argue for it in the abstract. I will take five changes that come to every program sooner or later, and show what Cone lets you do when they arrive. The compiler is part of each story, because a change that is easy to make is only worth having if the compiler also tells you what it broke.
Five principles
Five ideas make up what I mean by a properly architected system. Decomposition: the program is divided into parts, each with one job. Isolation: each part keeps an interface that is separate from the rest of its design, saying how other parts talk to it, and nothing outside can come to depend on anything else. Similarity: parts that are alike enough can be interchanged, which is what a shared interface, a trait, is. Varying a shape: the same logic works for a number of types, which is what generics are. And composing: parts and algorithms are combined in different ways, without a family tree to inherit through. Cone has mechanisms for each. The stories below use them, and I name each as it appears.
A value's memory strategy changes
A profile says that reference counting costs something in a program where little is actually shared. I want songs to have a single owner instead. In most languages that is a rewrite of every place that creates, copies or hands over a song. In Cone the strategy is part of a type, and I named the type once, with an alias. (The memory page has the strategies themselves. Here I only change my mind.)
Here is the program before. Two lists share the anthem, and a song is counted:
struct Song {
pub title: &str;
pub secs: i32;
}
// How the program holds a song, named once
alias Track = Rc[imm, Song];
fn total(list: &List[Track]) i32 {
var sum = 0;
each t in list.iter() { sum += t.secs; }
sum
}
fn main() {
var morning = List[Track].empty();
var evening = List[Track].empty();
let anthem = Track("anthem", 180);
morning.push(Track("ballad", 240));
morning.push(anthem);
evening.push(anthem); // the same song is on both lists
print <- "morning ", total(&morning), ", evening ", total(&evening), "\n";
}
modularity_alias_rc.cone (its module and import lines left out)
morning 420, evening 180
The change
One line:
alias Track = Uni[Song]; // was Rc[imm, Song]
modularity_alias_uni_refused.cone, line 11
Nothing else is touched. Track("ballad", 240), the lists, t.secs and the borrowed &List[Track] that total takes all read exactly as before, because a function that borrows does not know how what it borrows is owned.
What the compiler checks
My guess that little was shared was wrong in one place. The anthem was on both lists, and a single owner cannot be on two:
Error 1048: This variable's value has been moved out. It is no longer there to use.
--> evening.push(anthem); // the same song is on both lists
^--- modularity_alias_uni_refused.cone:25:16
Try it: the refused second list
The compiler did not decide for me. It showed me the one place where my design had been counting on sharing, and I decided: the evening list gets an anthem of its own.
morning.push(Track("ballad", 240));
morning.push(Track("anthem", 180));
evening.push(Track("anthem", 180)); // the evening list has an anthem of its own
modularity_alias_uni.cone, lines 22 to 24
morning 420, evening 180
A change of memory strategy is a one-line edit, and everywhere the line changes what is true, you are shown the place. The memory page tells this story with its own example, because it is where the cost of a choice comes due. Here it is a change of mind that stays small.
A type you don't own gains a capability
A library gives me a Meter. Its scale is private. My program must now pass meters to code that wants anything with an area(), and Meter has no such method. I cannot edit the library.
mod meters;
pub struct Meter {
pub var cm: i32;
scale: i32;
pub fn scaled(self) i32 { cm * scale }
pub fn make() Meter { Meter(4, 5) }
}
meters.cone, the library's, untouched throughout (modularity_foreign_after)
The change
Before, the program called show(Meter.make()). After, I declare a new type that extends Meter and gives it the method, a trait that names what I need, and a function over anything that meets the trait:
trait Area { pub fn area(self: &) i32; }
// Our own name for their type, plus the one method the trait needs
struct Framed extends Meter {
pub fn area(self: &) i32 { cm * cm }
}
fn areaOf(a: &<Area) i32 { a.area() }
fn show(m: Meter) {
print <- "scaled: ", m.scaled(), "\n";
}
fn main() {
let f: Framed = Meter.make(); // their constructor, our name
show(f); // still passes where a Meter is wanted
print <- "area: ", areaOf(&f), "\n";
}
modularity_foreign_after (its module and import lines left out)
scaled: 20 area: 16
Try it: a Meter that has an area
Three things happened without my editing anything of theirs. Framed has Meter's fields and methods, plus mine. The trait is met by shape, since Framed has an area of the right signature, so nothing says Framed is Area and the trait could have been written after the types, by someone else. And a Framed and a Meter are the same thing underneath, so a value passes from one to the other, both ways, with no conversion running.
What the compiler checks
The extension is not a way around privacy. It is inside the base type's boundary, so it may read the fields a Meter hides. Ordinary code, even code that handles a Meter, may not:
fn peek(m: Meter) i32 {
m.scale
}
modularity_foreign_refused, lines 6 to 8
Error 1042: May not access the private method/field `scale`.
--> m.scale
^--- modularity_foreign_refused.cone:7:4
Try it: reading a private field
That has a price, and the manual says so plainly. A type that may be extended has its representation in its contract: if a library's author renames a private field, everyone who extended the type finds out. I think the trade is right for the cases where you need it, and the library's author knows what they publish when they publish a type others may extend.
Work moves between an actor and a parallel loop
An actor scales a list of values in a plain loop. The list has grown, and the loop now holds up every other message the actor should be answering. Before:
actor Scaler {
vals: List[u64];
scale: u64;
pub async fn run(self, add: u64) {
let k = add * 2u64;
each v in vals.mutItems() {
*v = *v * scale + k;
}
print <- "scaled: ", vals[0], " ", vals[1], " ", vals[2], "\n";
}
}
modularity_scaler_seq.cone, lines 6 to 16
The change
parallel each v in vals.mutItems() { // was: each v in vals.mutItems()
*v = *v * scale + k;
}
modularity_scaler_par.cone, lines 11 to 13
scaled: 20 30 40
Try it: the loop spread over cores
One word. The compiler cuts the behavior where the loop ends. The actor is not run again until the passes have finished, and the rest of the behavior runs afterward. (The concurrency page has the machinery.) Nothing that sends this actor a message needs to know.
What the compiler checks
The word is only safe when the passes are independent, and the compiler checks that they are. A body that writes one total from every pass is a data race, and it is refused:
var total = 0u64;
parallel each v in vals.mutItems() {
*v = *v * scale + add;
total += *v; // every pass would write the same total
}
modularity_scaler_refused.cone, lines 10 to 14
Error 1382: A 'parallel each' body may not lend for writing total, which is declared outside the loop: the passes run at the same time. Outside variables are read-only for the loop. Hand results back with the parallel builder, or 'mutItems()' on the items, or count with an Atomic.
--> total += *v; // every pass would write the same total
^--- modularity_scaler_refused.cone:13:7
The parallel builder, the way to hand results back out of such a loop, is not yet built inside an actor's behavior, so a loop in a behavior can change its items in place, as here, but cannot yet build a new list.
A dependency adds a name that clashes
A car is built on an engine library. The car hands the engine's names on as its own, so callers write car.thrust(10.) and never mention the engine. It also has a power() of its own. This is name folding: one use clause says that these names, from that part, are now names here.
struct Car {
pub engine: Engine use *; // all of the engine's names are the car's
pub fn power(self: &) f32 { torque * 3. } // the car's own
}
fn main() {
var c = Car(Engine(50., 300.));
c.thrust(10.);
print <- "fuel ", c.fuel, " power ", c.power(), "\n";
}
modularity_fold_v1 (its module and import lines left out)
fuel 40 power 900
Try it: a car that folds its engine
The change
Engine 2.0 arrives, and the library's author has added a method named power. In some languages, my car's power() and the new one would now disagree somewhere, and the first sign would be a wrong answer. In Cone, the first sign is this:
Error 1011: power is already a name of Car. A folded name must be unique: rename it with 'as', or leave it out with 'but'.
--> pub engine: Engine use *; // all of the engine's names are the car's
^--- modularity_fold_v2_refused.cone:6:22
The failure comes where I wrote the clause, not at some call three modules away, and the repair is the same clause:
pub engine: Engine use * but power; // the car keeps its own power()
modularity_fold_v2_fixed.cone, line 6
fuel 40 power 900
That repair is tied to the upgrade. Naming a member that the old version does not have is itself an error, so but power goes in with Engine 2.0 and not before.
Swapping the part
The same clause lets me change the part itself. Suppose the car should run on a battery. Before, with an engine that exposes fuel and thrust:
struct Car {
pub drive: Engine use fuel, thrust;
pub wheels: i32;
}
fn cruise(c: &mut Car) {
c.thrust(12.5);
}
modularity_fold_swap_before, lines 5 to 12
fuel left: 37.5
After, one line, and cruise, and every caller of c.thrust, is unchanged:
pub drive: Battery use fuel, thrust; // was: Engine use fuel, thrust
modularity_fold_swap.cone, line 6
fuel left: 38.75
If the new part lacks a name that the clause lists, the clause is refused. The folding keeps both ends of the seam honest.
Two independent extensions combine
One library enriched Meter with trigonometry, and another with logarithms, and neither knows the other. Both gave it a method called span, and I want the methods of both. Because enrichment is also a use clause, the same rules apply, and the clash is settled once, in my own declaration:
struct Rich extends Meter {
use Trig but tangent, span;
use LogExp logOf, span as wide;
pub fn both(self) i32 { sine() + logOf() }
}
modularity_combine, lines 8 to 13
both: 29 wide: 16
Try it: two libraries, one Meter
Without the renames, the compiler stops at my declaration, and neither library is touched:
Error 1011: span is already a name of Rich. A folded name must be unique: rename it with 'as', or leave it out with 'but'.
--> use LogExp;
^--- modularity_combine_refused.cone:9:3
Try it: the refused combination
No inheritance tree was involved. Two parts were composed, and the combination was a declaration I could read.
Reuse: one template, many versions
The stories so far are about change. Reuse is the other half of the same idea: a part written once is used again somewhere else, and Cone gives it a few ways to be. I think of name folding, generics and macros as one family, because each one takes a pattern written once and makes it fit more places.
A generic leaves a type open. The compiler makes a version for each type the code meets, so a call into it is as direct as a call to hand-written code. A macro goes one step closer to the source: its logic is written into the program at each use, with the arguments put in for its parameters. Macros are typed, and hygienic, so their names stay out of the code around them, and the code around them stays out of theirs. A macro can also take types, to do the same logic over values of several types:
// A generic function: one template, a version for each type it meets
fn maxOf[T](a: T, b: T) T {
if a > b {a} else {b}
}
// A generic type: the pair works for any one type
struct Pair[T] {
pub first: T;
pub second: T;
pub fn larger(self: &) T {
maxOf(first, second)
}
}
// A typed macro: its logic is written into the program at each use
macro clamp[T][x: T, lo: T, hi: T] {
if x < lo {lo} else if x > hi {hi} else {x}
}
fn main() {
print <- maxOf(4, 6), " ", maxOf(2.5, 1.5), "\n";
let sizes = Pair[i64](10i64, 3i64);
print <- sizes.larger(), "\n";
print <- clamp(15, 0, 10), " ", clamp(0.25, 0.0, 1.0), "\n";
}
modularity_reuse.cone (its module and import lines left out)
6 2.5 10 10 0.25
Try it: generics and a typed macro
Generics reach beyond functions and types. A module may be generic, and each instance of it has its own globals. A third step, metaprogramming, which would decide what logic is compiled at all, is planned and is not built.
Generics and macros copy a pattern. Name folding, as you saw, composes parts, and it does it with the same checks. Between them, I have not needed an inheritance tree. I will say what it costs, since every one of these couples the template to its uses. A template, its versions and the code that uses them all depend on one another, which makes a program harder to follow and more brittle under change. I reach for the template when the pattern is truly the same, and for the trait from the first story when I only need the parts to be interchangeable.
The same principles at every layer
I used a function, a type and a loop in the stories above, and a module and an actor sit beside them. The five principles show up at each of these sizes, and the words Cone uses are the same ones. A type folds in a field's names with use, and a module folds in another's with the same word. A type meets a trait, and a module meets a module trait, so a whole module (its functions, its globals, its defaults) can be swapped behind an interface that is checked in one place. A type is generic, and so is a module. An actor keeps private state behind a public face that is only behaviors, so one can be split in two without touching state no one else could reach. That an actor has none of the reuse mechanisms yet, no use, no extends, no type parameters, no trait of its own, is not built yet. For now, shared logic goes into structs or modules that the actor owns. I find the consistency helps, because what I learn for one layer I can use for the next. But it is a support for the stories, not the point of them. The point is what you can do on the day the requirements change.
Further reading
These links point at the Cone manual:
- Modularity: dividing a program into parts.
- Extensibility, name folding and enrichment and Namespaces.
- Generics and macros and Substitution and traits.
- Public and private.
Why I built Cone · Also: Memory, Concurrency, Safety, Data in and out