Domain
The domain is the whole program, declared once, outside every context. Like a function, it has a contract and a body.
The contract
The contract holds the context map: every context, with arrows from upstream to downstream. A context may only import from contexts upstream of it, and the arrows may not go round in a circle.
contexts {
Dungeon -> Scoring
Dungeon -> Platform
}The contract also imports what the body needs, and names the ports it calls, with the context they come from.
import { SetUpRooms, PlayTurn, FIRST_ROOM } from Dungeon
import { SetUpScoring } from Scoring
calls { Keyboard as keyboard } from DungeonThe body
The body runs first. Any error passed up with ? stops the program with exit code 1, printing the error's name on standard error.
Here is the whole domain, contract and body together.
// game.croft
domain Game {
// upstream -> downstream: a context only imports from contexts upstream of it
contexts {
Dungeon -> Scoring
Dungeon -> Platform
}
import { SetUpRooms, PlayTurn, FIRST_ROOM } from Dungeon
import { SetUpScoring } from Scoring
calls { Keyboard as keyboard } from Dungeon
} = {
SetUpRooms()?
SetUpScoring()?
loop {
let pressed = keyboard.pressed()
if pressed == Some(Quit) {
break
}
PlayTurn(FIRST_ROOM, pressed)?
}
}Starting data
The body only sees what contexts export, so each context sets up its own starting data. Dungeon exports a service for the domain to call.
// in context Dungeon, exported for the domain
service SetUpRooms(): Result<Unit, GameError> {
calls { Rooms }
} = {
let player = Room.Player { id: 1, position: Position { x: 16, y: 16 }, lives: 3 }
let room = Room.Open(id: FIRST_ROOM, player: player, exit: Position { x: 232, y: 168 })?
Rooms.save(room)?
}