BjoManual/_02_.Writing-programs.sz

1276 lines
59 KiB
Text
Raw Normal View History

2026-10-01 11:36:18 +02:00
@section{Writing programs}
The first part was about the language itself, one file at a time. This part is about everything around it: splitting a program into modules, making it a project that can depend on other people's code, reading and writing files, doing several things at once, talking to .NET, extending the language with macros, and testing what you wrote.
The card game comes along. By the end of this part it is a project with a deck module, a high-score file, players that play concurrently, and a @code{#card(Q hearts)} literal.
@subsection{Modules}
Every @code{.bjo} file is a module. A module decides what other modules get to see by exporting it, and everything it does not export stays private. That is the whole model: there are no namespaces inside a file, and no module declaration at the top. The file name is the module name.
@subsubsection{Splitting the game into files}
Here is the deck from the first part as a module of its own. Put it in @code{deck.bjo}:
@codeblock[#:lang "bjolang"]|{
(import (std random))
(export Suit Rank Card full-deck shuffle card-points)
(type/derive (Eq Ord)
(: Suit (Union Clubs Diamonds Hearts Spades))
(: Rank (Union (: Pip int) Jack Queen King Ace))
(: Card (Struct (: rank Rank) (: suit Suit))))
(impl (->str Suit)
(defun (->str s)
(match s (Clubs "♣") (Diamonds "♦") (Hearts "♥") (Spades "♠"))))
(impl (->str Rank)
(defun (->str r)
(match r
((Pip n) (int->string n))
(Jack "J") (Queen "Q") (King "K") (Ace "A"))))
(impl (->str Card)
(defun (->str c) #"${(record-ref c rank)}${(record-ref c suit)}"))
;; Not exported: only this module needs them.
(def all-suits [Clubs Diamonds Hearts Spades])
(def all-ranks [(Pip 2) (Pip 3) (Pip 4) (Pip 5) (Pip 6) (Pip 7) (Pip 8)
(Pip 9) (Pip 10) Jack Queen King Ace])
(: full-deck (-> (Vec Card)))
(defun (full-deck)
(loop (:for s all-suits)
(:subloop)
(:for r all-ranks)
(:acc deck (vecing (Card (rank r) (suit s))))
=> deck))
(: shuffle (-> (Vec Card) (Vec Card)))
(defun (shuffle deck) (shuffle-vec deck))
(: card-points (-> Card int))
(defun (card-points c)
(match c
((Card (rank (Pip n))) n)
((Card (rank Ace)) 11)
(_ 10)))
}|
And the game, in @code{game.bjo} next to it:
@codeblock[#:lang "bjolang"]|{
(import "deck.bjo")
(defun (main)
(def hand (vec-slice (shuffle (full-deck)) 0 5))
(println #"Your hand: ${hand}")
(println #"Points: ${(vec-fold (fun (c acc) (+ acc (card-points c))) 0 hand)}")
0)
}|
@code{bjo run game.bjo} prints something like @code{Your hand: [3♥ 10♠ 8♥ 10♦ 7♣]}. A few things to notice:
@read-list{
- @code{(import "deck.bjo")} is a path, relative to the file that imports it. The imported file is compiled to @code{deck.dll} first, and rebuilt whenever it (or anything it includes) changes.
- Exporting a type exports all of it: the union's cases, the struct's constructor and its fields. @code{game.bjo} can write @code{(Card (rank Ace) (suit Spades))} and match on it.
- The @code{->str} implementations were not in the export list, and still work in @code{game.bjo}. An @code{impl} has no name, so it cannot be exported. It travels with the module that wrote it.
- @code{all-suits} was not exported, so @code{game.bjo} cannot see it.
}
Every exported binding needs a signature in the module that defines it, since the signature is what gets written into the compiled @code{.dll} for other modules to read:
@codeblock[#:lang "text"]|{
Export Error: Exported item 'helper' is missing a mandatory type signature at bad1.bjo:1
}|
@subsubsection{The standard library, and the prelude}
A module path in a list, like @code{(std random)}, names a module of the standard library. These are found relative to the compiler installation, not the current directory, so @code{(std random)} means the same file wherever you run @code{bjo} from. Some of them:
@read-table{
| Module | What it has |
|---------------------------------+------------------------------------------------------|
| @code{(std random)} | random numbers, @code{shuffle-vec} |
| @code{(std set)} | sets; @code{(std orderedset)}, @code{(std orderedmap)} |
| @code{(std ports)} | reading a whole port: @code{port->lines} and friends |
| @code{(std fmt)} | text layout |
| @code{(std rx)} | regular expressions |
| @code{(std run)} | running other programs |
| @code{(std effect)} | effect handlers |
| @code{(std simpletest)} | tests |
| @code{(std syntax-match)} | writing macros |
| @code{(text json)} | JSON |
}
The prelude, @code{(std prelude)}, is imported into every module without asking. That is where @code{println}, @code{list-map} and nearly everything in the first part comes from. If you want to get rid of a name from it, import it yourself with a modifier (modifiers are below), and the implicit import is dropped:
@codeblock[#:lang "bjolang"]|{
(import (except (std prelude) list-map))
}|
@subsubsection{Exporting types}
A type is only visible to an importer if it is in the export list. A module cannot export a function whose signature mentions a type it keeps to itself, since the importer would have no way to read that signature. That is an error where the library is built, not where it is used.
Sometimes you want importers to be able to hold on to a value without being able to look inside it. That is what @code{#:opaque} is for. Here is a pile of cards, in @code{pile.bjo}, that you can only draw from the top:
@codeblock[#:lang "bjolang"]|{
(import "deck.bjo")
(export Pile new-pile draw pile-size)
;; Importers can hold a Pile, but only this module can look inside.
(type (: Pile #:opaque (Record (: cards (Vec Card)))))
(: new-pile (-> Pile))
(defun (new-pile) (Pile (cards (shuffle (full-deck)))))
(: pile-size (-> Pile int))
(defun (pile-size p) (vec-length (record-ref p cards)))
;; The top card and the rest of the pile, or None when it is empty.
(: draw (-> Pile (Option (Tuple Card Pile))))
(defun (draw p)
(match (record-ref p cards)
([] None)
([top rest ...] (Some (Tuple top (Pile (cards rest)))))))
}|
Another module can call @code{new-pile}, @code{draw} and @code{pile-size}, and write @code{Pile} in its signatures, but it cannot build one with @code{(Pile ...)}, match on it, or read its fields:
@codeblock[#:lang "text"]|{
Type Error at game3.bjo:4: 'cards' cannot be read here. pile/Pile is exported #:opaque,
so its representation is visible only to the code of pile. A value of it can be held and
passed on here, and built and taken apart through the functions that module exports.
}|
The @code{impl}s of an opaque type still work everywhere, so if @code{pile.bjo} had an @code{(impl (->str Pile) ...)}, printing a pile would work in any module.
@subsubsection{Import modifiers}
An import can be wrapped in modifiers that decide what the names it brings in are called, and which of them arrive:
@read-table{
| Modifier | Does |
|---------------------------------+-------------------------------------------------------|
| @code{(only m a b ...)} | only these functions and macros |
| @code{(except m a b ...)} | everything but these |
| @code{(prefix m "p/")} | puts @code{p/} in front of every name |
| @code{(postfix m "/p")} | the same, at the end |
| @code{(prefix-defs m "p/")} | a prefix on functions and macros only |
| @code{(prefix-types m "P/")} | a prefix on types, constructors and traits only |
| @code{(rename m (old new) ...)} | renames functions and macros |
}
@codeblock[#:lang "bjolang"]|{
(import "deck.bjo"
(prefix "pile.bjo" "pile/"))
(defun (main)
(match (pile/draw (pile/new-pile))
((Some (Tuple c rest)) (println #"Drew ${c}, ${(pile/pile-size rest)} left"))
(None (println "empty")))
0)
}|
Modifiers nest, and are read inside out: @code{(prefix (except (std set) set-map) "s/")} drops @code{set-map} and prefixes the rest.
@code{only} and @code{except} work on functions and macros, never on types. The types a module exports always arrive, because the signatures of the functions that do arrive might need them:
@codeblock[#:lang "bjolang"]|{
(import (only "deck.bjo" full-deck)
(rename (std random) (shuffle-vec mix)))
(defun (main)
(println (vec-length (mix (full-deck)))) ;; 52
(println (Card (rank Ace) (suit Spades))) ;; A♠, Card came along anyway
0)
}|
A type belongs to the module that declared it. If two libraries both declare a @code{Card}, those are two different types, and the bare name @code{Card} means the one from the later import. @code{prefix-types} gives each of them a name of its own:
@codeblock[#:lang "bjolang"]|{
(import (prefix-types "poker.bjo" "P/")
(prefix-types "bridge.bjo" "B/"))
(: convert (-> P/Card B/Card))
}|
@subsubsection{Which name wins}
When two things have the same name, three rules decide, in this order:
@read-list{
- A name defined in the module itself wins over anything imported.
- Between two imports, the later one wins, silently. This is what lets any module override the prelude: the prelude is always the first import.
- A name that a modifier or an alias made up may not collide with anything else. That is an error, since you asked for that name by hand and two answers mean the request was ambiguous.
}
@subsubsection{re-export and :alias}
@code{export} publishes what a module defines. @code{re-export} publishes something the module imported, which is how you write a module that gathers several others behind one import. The prelude passes on @code{=}, @code{compare} and @code{list-sort} from @code{(std eq)} this way. For the game:
@codeblock[#:lang "bjolang"]|{
;; cards.bjo: one import for everything about cards
(import "deck.bjo" "pile.bjo")
(re-export Card Rank Suit full-deck shuffle Pile new-pile draw)
}|
A re-exported type is the same type, not a copy, so a @code{Card} made through @code{cards.bjo} is a @code{Card} to @code{deck.bjo} as well. The @code{impl}s do not come along though: they travel with the module that wrote them, so a program that wants to print cards has to import @code{deck.bjo} too.
@code{(:alias new old)} gives a binding or a macro a second name. Together with a re-export or an export, that is how a library publishes something under a nicer name than it was written under:
@codeblock[#:lang "bjolang"]|{
(:alias deal full-deck)
(export deal)
}|
@subsubsection{include}
@code{(include "file.bjo")} pastes the file's forms in where the include is, as if you had written them there. No module is made, nothing needs exporting, and everything in the included file is in scope directly:
@codeblock[#:lang "bjolang"]|{
;; helpers.bjo
(: double (-> int int))
(defun (double x) (* x 2))
}|
@codeblock[#:lang "bjolang"]|{
(include "helpers.bjo")
(defun (main)
(println (double 21)) ;; 42
0)
}|
The rule of thumb: use @code{include} to split one module that has grown too big across several files, and @code{import} when the other file is a module in its own right, with its own idea of what is public.
@subsection{Projects and packages}
So far every program has been a file, and the files it imports are found by path. That works fine for small things. For anything that depends on someone else's code, or that you want others to depend on, make it a project.
A project is a directory with a @code{manifest.bjodat} in it. Inside a project @code{bjo} knows what your code is called, what it needs and where to get it. Outside one, every command works on the file you give it, exactly like before.
@subsubsection{Making a project}
@codeblock[#:lang "text"]|{
mkdir cards && cd cards
bjo init
bjo run
}|
@code{bjo init} makes this:
@codeblock[#:lang "text"]|{
cards/
manifest.bjodat what the package is called, and what it needs
src/
main.bjo the program
tests/
.gitignore
}|
and the manifest says what the package is called:
@codeblock[#:lang "bjolang"]|{
(package
(name (cards))
(version "0.1.0"))
}|
All the modules of a package live in @code{src/}, and a module's name is its path below it. So if you move @code{deck.bjo} into @code{src/}, it becomes the module @code{(cards deck)}, and @code{src/main.bjo} imports it like any library module:
@codeblock[#:lang "bjolang"]|{
(import (cards deck))
(defun (main args)
(println #"Your hand: ${(vec-slice (shuffle (full-deck)) 0 5)}")
(println #"Arguments: ${args}")
0)
}|
@code{bjo} finds the project by looking upwards for a manifest, so every command works from anywhere inside it.
@read-table{
| Command | What it does |
|---------------------------+----------------------------------------------------------------|
| @code{bjo init [--lib]} | makes a project in the current directory |
| @code{bjo fetch} | fetches the dependencies, compiles nothing |
| @code{bjo build} | fetches, then compiles the program, or every module of a library |
| @code{bjo run . args ...} | builds, then runs the program with the arguments |
| @code{bjo check} | reports every error in the project, and builds nothing |
| @code{bjo repl} | the REPL, where the project's modules can be imported |
}
The @code{.} in @code{bjo run . one two} stands for "the project". Everything after the file (or the dot) is passed to your program, so here @code{args} is @code{[one two]}. A file named inside a project, like @code{bjo run tests/deck-test.bjo}, is compiled against the project's packages, which is how you run tests.
@subsubsection{Libraries}
A package whose entry file, @code{src/main.bjo}, does not exist is a library. @code{bjo init --lib} makes one, and @code{bjo build} in it compiles every module under @code{src/}.
So let us make the deck a library of its own, in a directory next to the game:
@codeblock[#:lang "text"]|{
mkdir cardlib && cd cardlib
bjo init --lib
mv ../cards/src/deck.bjo src/
}|
The game then depends on it by path, in @code{cards/manifest.bjodat}, and imports it as @code{(cardlib deck)}:
@codeblock[#:lang "bjolang"]|{
(package
(name (cards))
(version "0.1.0")
(depends
(package (name (cardlib))
(source (path (dir "../cardlib"))))))
}|
@codeblock[#:lang "bjolang"]|{
(import (cardlib deck))
(defun (main)
(println (vec-slice (shuffle (full-deck)) 0 5))
0)
}|
A path is relative to the manifest that names it. A path dependency has no version: what is in the directory is what you get, which is what you want while you are working on both.
@subsubsection{Dependencies from git}
When the library is published somewhere, depend on it through git instead:
@codeblock[#:lang "bjolang"]|{
(depends
(package (name (cardlib))
(version (version-at-least "0.1.0"))
(source (git (url "https://github.com/someone/cardlib")))))
}|
A version is a git tag of exactly the form @code{vX.Y.Z}, and a git dependency must say which versions it takes. There are three ways to say it: @code{(version-at-least "1.2")}, @code{(version-between "1.2" "2.0")} and @code{(version-at-most "2.0")}.
Versions are chosen by minimal version selection, which is simpler than it sounds. Every package in the graph says the lowest version it needs of each of its dependencies, and the version picked is the highest of those. Not the newest version that exists, but the lowest one that everyone who asked can live with. This means that a new release of a dependency changes nothing in your build until some manifest asks for it, and that the same manifests build the same code a year from now. An upper bound is only ever checked, never used to pick an older version.
@code{bjo fetch} (and @code{build} and @code{run}, which fetch first) clones what is needed into @code{.bjo/} and writes @code{bjo.lock}, with the exact commit each dependency resolved to. Commit the lock file, and do not commit @code{.bjo/} (the generated @code{.gitignore} already leaves it out). If a tag is moved to a different commit after it was locked, the build stops rather than quietly using different code under the same version. @code{--locked} turns any difference from the lock file into an error, which is what you want on a build server.
If your own manifest gives a dependency a source, that source is used everywhere in the graph, including for the dependencies of your dependencies. That is how you swap in a fork, or a path to a local checkout while you debug something.
@subsubsection{NuGet packages}
.NET libraries from NuGet are listed under @code{packages}:
@codeblock[#:lang "bjolang"]|{
(package
(name (shop))
(version "0.1.0")
(packages (nuget (id "Npgsql") (version "9.0.3"))))
}|
Their types are then used like any other .NET type (see the .NET section below). The version means what NuGet says it means: @code{"9.0.3"} is that version or newer, and @code{"[9.0.3]"} is exactly that version. NuGet packages are restored by the .NET SDK, so this needs the @code{dotnet} command, and what they resolved to is recorded in @code{packages.lock.json}, which should also be committed. For now, the built program loads the packages from the NuGet cache, so it only runs on the machine that built it.
@subsubsection{Publishing a package}
@read-list{
- Put the modules under @code{src/}, and write a manifest with a @code{name} and a @code{version}.
- @code{bjo build} to check that everything compiles.
- Tag the release, @code{git tag -a v0.1.0 -m "First release"}, and push the tag. A version that is not a @code{vX.Y.Z} tag cannot be depended on.
}
@subsection{Files and I/O}
@subsubsection{Whole files}
When a file fits in memory, the simplest thing is to read or write all of it at once:
@read-table{
| Function | Does |
|-------------------------------------------+---------------------------------------------------|
| @code{(file-read-text path)} | the whole file, as a string |
| @code{(file-write-text path s)} | writes @code{s}, replacing what was there |
| @code{(file-append-text path s)} | adds @code{s} at the end, creating the file |
| @code{(file-read-lines path)} | the lines, as a @code{(Vec string)} |
| @code{(file-write-lines path lines)} | writes a @code{(Vec string)}, one line each |
| @code{(file-read-bytes path)} | the whole file, as an @code{(Array byte)} |
| @code{(file-exists? path)} | whether there is a file there |
| @code{(file-delete path)} | deletes it; a missing file is not an error |
| @code{(file-copy from to)} | copies, and fails rather than overwrite @code{to} |
| @code{(file-move from to)} | moves a file or a directory |
| @code{(file-info path)} | size, modified time and kind, as an @code{Option} |
}
These throw when something goes wrong, the way the .NET methods under them do. Wrap a call in @code{try} when a failure is something you expect.
Here is a high-score file for the game. Each line is a name and a number, and a line that is not one is skipped rather than crashing the game:
@codeblock[#:lang "bjolang"]|{
(: score-file string)
(def score-file "scores.txt")
(: parse-score (-> string (Option (Tuple string int))))
(defun (parse-score line)
(match (string-split line " ")
([name points]
(match (try (string->int points) #:catch (System.FormatException))
((Ok n) (Some (Tuple name n)))
((Err _) None)))
(_ None)))
(: read-scores (-> (Vec (Tuple string int))))
(defun (read-scores)
(if (file-exists? score-file)
(loop (:for line (file-read-lines score-file))
(:when-let (Some score) (parse-score line))
(:acc (vecing score)))
[]))
(: save-score (-> string int void))
(defun (save-score name points)
(file-append-text score-file #"${name} ${points}\n"))
(defun (main)
(save-score "ada" 31)
(save-score "bo" 27)
(println (read-scores)) ;; [(ada, 31) (bo, 27)]
0)
}|
@subsubsection{Paths, directories and the environment}
Paths are strings, and the path functions only work on the text. They never touch the disk:
@codeblock[#:lang "bjolang"]|{
(path-combine "a" "b" "c.txt") ;; "a/b/c.txt"
(path-file-extension "deck.bjo") ;; (Some ".bjo")
(path-filename "/tmp/x/") ;; None: it names a directory
(path-directory "/tmp/x/y.txt") ;; (Some "/tmp/x")
(path-absolute "deck.bjo") ;; the full path
}|
The directory functions do touch the disk:
@codeblock[#:lang "bjolang"]|{
(directory-create "saves/2026") ;; creates every missing directory on the way
(directory-exists? "saves") ;; #t
(directory-files ".") ;; the files directly in ".", as a (Vec string)
(directory-subdirectories ".") ;; and the directories
(directory-delete-tree "saves") ;; everything under it, and it
;; There is no pattern argument. Filter instead:
(vec-filter #(string-ends-with? & ".bjo") (directory-files "."))
;; Every file under a directory, lazily, without going into .git:
(filter #(string-ends-with? & ".bjo")
(directory-walk "." #:into? (fun (d) (not (= (path-filename d) (Some ".git"))))))
}|
And the environment:
@codeblock[#:lang "bjolang"]|{
(get-environment-variable "HOME") ;; (Some "/home/linus"), or None
(set-environment-variable! "MODE" "x") ;; for this process and the ones it starts
(current-directory)
}|
@subsubsection{Ports}
For files that are too big to read at once, or for reading and writing a bit at a time, there are ports. A text input port is something you can read lines and characters from, and an output port is something you can write to. A file port and a string port are the same type, so a function that reads from a port does not care where the text comes from.
Opening a file answers a @code{Result}, since a missing file is something you should expect. The @code{Err} holds the real .NET exception, so you can tell the failures apart with @code{:is}:
@codeblock[#:lang "bjolang"]|{
(match (open-input-file "nope.txt")
((Ok port) (read-all port))
((Err (:is System.IO.FileNotFoundException e)) #"missing: ${(.-FileName e)}")
((Err e) "some other error"))
}|
The easiest way to use a port is to let @code{call-with-input-file} or @code{call-with-output-file} open it, hand it to a function, and close it however that function ends. Here is counting the lines of a file of any size:
@codeblock[#:lang "bjolang"]|{
(: count-lines (-> string (Result Exception int)))
(defun (count-lines path)
(call-with-input-file path
(fun (port)
(loop (:for i (up-from 0))
(:break-let (Some line) (read-line/opt port))
(:acc lines (counting line))
=> lines))))
(count-lines "scores.txt") ;; (Ok 2)
}|
@read-table{
| Function | Does |
|-----------------------------------+---------------------------------------------------------|
| @code{(open-input-file path)} | a @code{(Result Exception TextInputPort)} |
| @code{(open-output-file path)} | the same for writing; @code{#:mode} is @code{Truncate}, @code{Append} or @code{CreateNew} |
| @code{(read-line/opt p)} | the next line, or @code{None} at the end |
| @code{(read-char/opt p)} | the next character, or @code{None} at the end |
| @code{(read-all p)} | everything that is left, as one string |
| @code{(port-eof? p)} | whether there is nothing more to read |
| @code{(write-string p s)} | writes @code{s} |
| @code{(writeln p s)} | writes @code{s} and a newline |
| @code{(close-input-port p)} | and @code{close-output-port} |
| @code{(open-input-string s)} | a port that reads from a string |
| @code{(open-output-string)} | a port that collects what is written to it; @code{get-output-string} reads it |
}
@code{read-line} and @code{read-char} also exist without the @code{/opt}. They throw at the end of the input.
@code{(std ports)} has the functions that read a whole port into a collection: @code{port->lines}, @code{port->list}, @code{port->vec} and the lazy @code{port->seq} and @code{file->seq}:
@codeblock[#:lang "bjolang"]|{
(import (std ports))
(call-with-input-file "scores.txt" port->lines) ;; (Ok ["ada 31" "bo 27"])
}|
A file port belongs to the scope it was opened in (scopes are in the concurrency section), and is closed when that scope ends, whether you closed it or not. @code{main} is a scope, so a port you forget is closed at the latest when the program ends. For anything long-running, close ports yourself or use @code{call-with-input-file}.
@subsubsection{Running other programs}
@code{(std run)} runs other programs. The command is written as a quoted list, and pipes and redirections are part of the same notation:
@codeblock[#:lang "bjolang"]|{
(import (std run))
(run/string '(echo "hello")) ;; (Ok "hello\n")
(run/strings '(pipe (cat "scores.txt") (sort -r))) ;; (Ok ["bo 27" "ada 31"])
(run/status '(into-file "sorted.txt"
(pipe (cat "scores.txt") sort))) ;; (Ok 0), the exit code
(def who "ada")
(run/strings '(grep ,who "scores.txt")) ;; (Ok ["ada 31"])
}|
@code{,who} puts the value of a variable into the command. Every word is one argument, spaces and all, so there is no quoting to get wrong. @code{pipe}, @code{into-file}, @code{from-file}, @code{append-to-file} and @code{errors-into-file} are part of the notation, and their arguments are checked when the program is compiled. Anything else at the head of a form is the name of a program to run.
@read-table{
| Function | Answers |
|---------------------------+------------------------------------------------------|
| @code{(run/string form)} | what the command wrote to stdout |
| @code{(run/strings form)} | the same, one string per line |
| @code{(run/status form)} | the exit code |
| @code{(run/output form)} | the exit code and stdout |
| @code{(run form)} | a running @code{Proc}, whose input and output are yours |
}
All of them answer a @code{Result}, and all of them block the thread they run on. Inside a bjoroutine there are versions that do not; see the documentation of @code{(std run)}.
@subsection{Concurrency}
Bjolang's concurrency is built on three ideas: lightweight threads called bjoroutines, channels to talk between them, and scopes that make sure nothing started inside them outlives them. The channel operations are Concurrent ML's, which means an operation like "receive from this channel" is a value that can be combined with others before you wait for it.
@subsubsection{Bjoroutines}
A function that may wait for something (a channel, a timer, a file) is defined with @code{defbjo} instead of @code{defun}. Its signature is written with @code{-bjo->}. Waiting is done with @code{sync}:
@codeblock[#:lang "bjolang"]|{
(: think (-bjo-> int int))
(defbjo (think n)
(sync (timeout 50)) ;; wait 50 ms, without holding up a thread
(* n n))
}|
A bjoroutine runs as a fiber on a pool of threads. When it waits, it gives its thread back, so ten thousand of them waiting on a channel cost ten thousand small objects, not ten thousand threads.
The catch is colour. Calling a bjoroutine is a place where the caller may have to wait too, so only a bjoroutine may call one:
@codeblock[#:lang "text"]|{
Type Error at colour.bjo:7: calling 'think' is a yield point, and a yield point is not allowed here.
'twice' is defined with (defun ...), which is emitted as an ordinary C# method, and an ordinary
method cannot await.
Define it with (defbjo ...), or move the suspending call out of it. Note that (defbjo ...) spreads:
whoever calls 'twice' needs to be one too.
}|
@code{main} may be a @code{defbjo}, and that is how a program gets into the world of bjoroutines.
The colour does not spread as far as you might fear. Most of the prelude's I/O, like @code{file-read-text}, is written so that it has two copies: one that blocks and one that waits. An ordinary @code{defun} that calls one of them gets two copies too, without you writing anything, and a bjoroutine calling it gets the one that waits:
@codeblock[#:lang "bjolang"]|{
(: size-of (-> string int))
(defun (size-of path) (string-length (file-read-text path))) ;; an ordinary defun
(defbjo (main)
(println (size-of "deck.bjo")) ;; the file is read without blocking a thread
0)
}|
@subsubsection{spawn and channels}
@code{(spawn (f args ...))} starts @code{f} as a new fiber and carries on. The arguments are evaluated where the @code{spawn} is written, and only the call happens in the new fiber.
A channel, made with @code{make-chan}, is how fibers talk. @code{(chan-send ch v)} and @code{(chan-recv ch)} are events, and @code{sync} performs them. A channel is a rendezvous: a send waits until someone receives, and the other way around.
Here are the players of the card game as bjoroutines. Each player gets a seat, which is a channel, and plays one card into it per trick, highest first. The table (@code{main}) takes one card from each seat in turn:
@codeblock[#:lang "bjolang"]|{
(import "deck.bjo")
(: player (-bjo-> (Vec Card) (Chan Card) void))
(defbjo (player hand seat)
(loop (:for c (list-reverse (list-sort (vec->list hand))))
(:do (sync (chan-send seat c)))))
;; One trick: a card from every seat, in turn.
(: play-trick (-bjo-> (Vec (Chan Card)) (Vec Card)))
(defbjo (play-trick seats)
(loop (:for seat seats)
(:acc (vecing (sync (chan-recv seat))))))
(defbjo (main)
(def deck (shuffle (full-deck)))
(def seats [(make-chan) (make-chan) (make-chan)])
(loop (:for seat seats)
(:for p (range 0 3))
(:do (spawn (player (vec-slice deck (* p 4) 4) seat))))
(loop (:for trick (range 1 5))
(:do (println #"Trick ${trick}: ${(play-trick seats)}")))
0)
}|
@codeblock[#:lang "text"]|{
Trick 1: [K♥ K♦ J♥]
Trick 2: [8♣ 7♣ 4♣]
Trick 3: [6♠ 4♥ 3♣]
Trick 4: [6♦ 3♦ 2♦]
}|
Since each send waits for its receive, a player cannot run ahead and play two cards into the same trick.
@subsubsection{Events are values}
@code{(chan-recv seat)} does not receive anything. It describes a receive that has not happened yet, and only @code{sync} makes it happen. Until then it is a value like any other, and can be passed around and combined:
@read-table{
| Function | Is the event that |
|---------------------------+-------------------------------------------------------------|
| @code{(chan-recv ch)} | receives a value from @code{ch} |
| @code{(chan-send ch v)} | sends @code{v} on @code{ch} |
| @code{(timeout ms)} | happens after @code{ms} milliseconds |
| @code{(choose e1 e2 ...)} | happens when the first of its events happens |
| @code{(wrap e f)} | happens when @code{e} does, and answers @code{f} of its value |
}
With those, a deadline is an ordinary function that works on any event at all. A player who takes too long forfeits:
@codeblock[#:lang "bjolang"]|{
(: within (-> int (Event %a) (Event (Option %a))))
(defun (within ms ev)
(choose (wrap ev #(Some &))
(wrap (timeout ms) (fun (u) None))))
(: slow-player (-bjo-> (Chan string) void))
(defbjo (slow-player seat)
(sync (timeout 200))
(sync (chan-send seat "7♣")))
(defbjo (main)
(def seat (make-chan))
(spawn (slow-player seat))
(println (sync (within 50 (chan-recv seat)))) ;; None
(println (sync (within 500 (chan-recv seat)))) ;; (Some 7♣)
0)
}|
Note that @code{within} is a @code{defun}. Building an event is not waiting for it, so no colour is involved until the @code{sync}.
The part worth noticing is the second line. When the timeout won the first race, the receive was withdrawn, not performed, so the card was not taken off the channel and lost. It was still there for the second try.
@subsubsection{Answers from a fiber: bjo}
@code{spawn} throws away what the function answers. @code{bjo} instead gives you a promise of it, which you can wait for with @code{promise-join}. The answer is a @code{Result}, since the fiber might have failed:
@codeblock[#:lang "bjolang"]|{
(def a (bjo (think 3)))
(def b (bjo (think 4)))
(sync (promise-join a)) ;; (Ok 9)
(sync (promise-join b)) ;; (Ok 16)
}|
Both fibers think at the same time, so this takes 50 ms and not 100.
@subsubsection{Scopes}
Every fiber is started inside a scope, and a scope does not end until every fiber started in it has ended. So when a scope returns, the work started inside it is over. Nothing keeps running in the background by accident, and if a fiber fails, the failure is reported when the scope ends instead of disappearing.
@code{main} is a scope, which is why the players above did not need anything special. You make more of them with these forms:
@read-table{
| Form | Is a scope that |
|---------------------------------------+------------------------------------------------------------|
| @code{(with-scope body ...)} | waits for its fibers, and closes its files |
| @code{(with-cancel (cancel) body ...)} | also binds @code{cancel}, which tells its fibers to stop |
| @code{(with-deadline ms body ...)} | is cancelled after @code{ms} milliseconds |
| @code{(with-shield body ...)} | is not cancelled when its parent is; for cleaning up |
}
@codeblock[#:lang "bjolang"]|{
(: fetch (-bjo-> string void))
(defbjo (fetch name)
(sync (timeout 30))
(println #"fetched ${name}"))
(: count-up (-bjo-> (Chan int) void))
(defbjo (count-up ch)
(let go ((i 0))
(sync (chan-send ch i))
(go (+ i 1))))
(defbjo (main)
;; Both are fetched when this returns, or the deadline raises.
(with-deadline 1000
(spawn (fetch "a"))
(spawn (fetch "b")))
(println "both done")
;; count-up never ends on its own. Cancelling the scope stops it
;; the next time it waits.
(def numbers (make-chan))
(with-cancel (cancel)
(spawn (count-up numbers))
(println (sync (chan-recv numbers))) ;; 0
(println (sync (chan-recv numbers))) ;; 1
(cancel (Requested "enough")))
0)
}|
Cancelling does not interrupt anything. A cancelled fiber notices the next time it waits for something, and a fiber in a loop that never waits never notices. A fiber that stopped because it was cancelled has not failed; that is how a worker normally ends.
There are four ways to start a fiber, and they differ only in what the scope does about it:
@read-table{
| Form | The scope |
|------------------------------------+----------------------------------------------------------|
| @code{(spawn (f x))} | waits for it, and reports it if it fails |
| @code{(bjo (f x))} | waits for it; the promise is yours, and so is the failure |
| @code{(spawn/daemon (f x))} | cancels it when the scope ends, but does not wait for it |
| @code{(spawn/detached (f x))} | has nothing to do with it |
}
The scope forms wait, so they can only be written in a bjoroutine.
@subsubsection{Work that is not a fiber}
A fiber that blocks its thread, with a .NET call that does not know about fibers, or with a long computation, holds up one of the threads all the other fibers share. There are two ways to move such work off the pool, and both give you an event:
@codeblock[#:lang "bjolang"]|{
;; Work that waits: a call that parks its thread.
(sync (blocking #(file-read-text "deck.bjo"))) ;; (Ok "...")
;; Work that computes: gets a thread of its own.
(sync (spawn/thread #(loop (:for i (range 0 1000)) (:acc (summing i))))) ;; (Ok 499500)
}|
The other direction also exists. An ordinary @code{defun} cannot @code{sync}, but it can wait for an event by parking its own thread with @code{sync/blocking}. Do not use it inside a bjoroutine, since the thread it parks is one the fibers need:
@codeblock[#:lang "bjolang"]|{
(: wait-for-card (-> (Chan Card) Card))
(defun (wait-for-card ch)
(sync/blocking (chan-recv ch)))
}|
@subsection{Effects}
The deck is shuffled with @code{shuffle-vec}, which is random. That is what you want when playing, and exactly what you do not want in a test that checks who wins a trick. The usual answer is to pass a shuffle function down to everything that needs one. Effects are the other answer: the function performs an operation, and whoever called it decides what the operation does.
@subsubsection{Declaring an effect}
@codeblock[#:lang "bjolang"]|{
(import (std effect) "deck.bjo")
(defeffect Dealing
(shuffle-deck (-> (Vec Card) (Vec Card)))
#:default ((shuffle-deck shuffle)))
(: deal-hand (-> int (Vec Card)))
(defun (deal-hand n)
(vec-slice (shuffle-deck (full-deck)) 0 n))
}|
@code{shuffle-deck} is now a function with the declared type, and @code{deal-hand} calls it like any other function. With nobody handling it, it does its default, which here is the real shuffle. A test installs another one with @code{with-handler}:
@codeblock[#:lang "bjolang"]|{
(deal-hand 5) ;; five random cards
(with-handler ((shuffle-deck (fun (deck) deck)))
(deal-hand 5)) ;; [2♣ 3♣ 4♣ 5♣ 6♣], every time
}|
The handler applies to everything called inside the @code{with-handler}, however deep, and to fibers spawned inside it. Nothing had to be passed through @code{deal-hand}.
A few things to know:
@read-list{
- An effect without a @code{#:default} raises an exception when it is performed and nobody handles it. Leave the default out when running without a handler is a mistake.
- A handler is called like a function, and answers once. There are no continuations: it cannot resume twice, or not at all, except by throwing.
- An operation declared with @code{->} can be performed from any function. One declared with @code{-bjo->} can only be performed from a bjoroutine, and needs a bjoroutine as its handler.
- Inside its own handler, performing an operation calls the handler again. @code{(handler-of op)} is the handler that was installed before, which is how a handler adds something and passes the rest on:
}
@codeblock[#:lang "bjolang"]|{
(with-handler ((log (let ((outer (handler-of log)))
(fun (s) (outer (str "[game] " s))))))
(log "dealing")) ;; "[game] dealing" on stderr
}|
@subsubsection{The prelude's own effects}
Some things in the prelude are effects already, so you can handle them without changing the code that uses them:
@read-table{
| Operation | Default |
|----------------------------+----------------------------------------------------------|
| @code{(log s)} | writes a line to stderr |
| @code{(warn s)} | writes a line to stderr |
| @code{(getenv name)} | reads an environment variable, as an @code{Option} |
| @code{(monotonic-ms)} | a clock for measuring time |
| the @code{FS} operations | the real file system |
}
Everything in the prelude that reaches the disk goes through the @code{FS} operations, so a test can replace the whole file system. That is @code{with-fake-fs}, which is in the testing section below.
Collecting the log lines of a piece of code, instead of printing them, is a handler:
@codeblock[#:lang "bjolang"]|{
(def lines (make-box (list)))
(with-handler ((log (fun (s) (box-set! lines (Cons s (box-ref lines))))))
(log "one")
(log "two"))
(box-ref lines) ;; '("two" "one")
}|
A @code{with-handler} does not wait for anything, unlike a scope. If the code inside it spawns fibers that log, put the scope that waits for them inside the handler, or you will read the lines before they are written.
@subsection{.NET}
Bjolang compiles to C#, so every .NET library is available. You tell the compiler which methods and classes you want and what their types are, and they become ordinary bjolang functions and types.
@subsubsection{Methods: import/extern}
Let us give the suits colours in the terminal. @code{System.Console} has what is needed:
@codeblock[#:lang "bjolang"]|{
(import "deck.bjo")
(import/class
(ConsoleColor (: System.ConsoleColor)))
(import/extern
(set-foreground! (: System.Console.ForegroundColor (-> ConsoleColor void) #:set))
(reset-colour! (: System.Console.ResetColor (-> void)))
(console-write (: System.Console.Write (-> string void))))
(: suit-colour (-> Suit ConsoleColor))
(defun (suit-colour s)
(match s
((or Hearts Diamonds) ConsoleColor.Red)
((or Clubs Spades) ConsoleColor.Blue)))
(: print-card (-> Card void))
(defun (print-card c)
(set-foreground! (suit-colour (record-ref c suit)))
(console-write #"${c} ")
(reset-colour!))
(defun (main)
(vec-for-each print-card (vec-slice (shuffle (full-deck)) 0 5))
(println "")
0)
}|
Each entry in @code{import/extern} is a name for bjolang, the full .NET name, and a type. Methods with many overloads, like @code{Console.Write}, are resolved from the type you give. An instance method takes the object as its first argument. @code{#:get} and @code{#:set} read and write a property or a field:
@codeblock[#:lang "bjolang"]|{
(import/extern
(trim (: System.String.Trim (-> string string)))
(max-int (: System.Int32.MaxValue #:get)))
(trim " hi ") ;; "hi"
max-int ;; 2147483647
}|
Do not give an import the name of a prelude function. A name the module binds itself, or gets from the prelude, wins over an @code{import/extern} alias, so an alias called @code{println} would never be used.
@subsubsection{Classes: import/class}
@code{import/class} makes a .NET class a bjolang type, and its constructor a function spelled with a dot at the end. Enums work the same way, and their members are written @code{Type.Member}, as with @code{ConsoleColor.Red} above.
@codeblock[#:lang "bjolang"]|{
(import/class
(StringBuilder (: System.Text.StringBuilder (-> StringBuilder))))
(def sb (StringBuilder.)) ;; new StringBuilder()
(ignore (.Append sb "hello, "))
(ignore (.Append sb "world"))
(.ToString sb) ;; "hello, world"
(.-Length sb) ;; 12
(.ToUpper "shout") ;; "SHOUT"
}|
As the last lines show, you do not have to import every method. @code{(.Method obj args ...)} calls a method, and @code{(.-Property obj)} reads a property, as long as the type of @code{obj} is known at that point. @code{Append} answers the builder again, and a value that is thrown away has to be thrown away on purpose, with @code{ignore}.
@subsubsection{When .NET fails}
.NET methods report failure by throwing. There are three ways to turn that into a value:
@read-list{
- @code{try}, around any code, which was covered in the first part.
- @code{#:exceptions} on a constructor in @code{import/class}, which makes the constructor answer a @code{Result}.
- @code{(out T)} parameters: a @code{TryParse}-style method, which answers a @code{bool} and puts its result in an @code{out} parameter, becomes a function that answers an @code{Option}.
}
@codeblock[#:lang "bjolang"]|{
(import/class
(Uri (: System.Uri (-> string Uri)
#:exceptions (System.UriFormatException))))
(import/extern
(parse-int (: System.Int32.TryParse (-> string (out int) (Option int)))))
(match (Uri. "https://example.com/cards")
((Ok u) (.-Host u)) ;; "example.com"
((Err e) (.-Message e)))
(parse-int "42") ;; (Some 42)
(parse-int "forty-two") ;; None
}|
@subsubsection{Async methods, and ones that block}
A .NET method that answers a @code{Task} is imported with @code{#:async}. Calling it from a bjoroutine waits for the task without blocking a thread, and you never write @code{await}:
@codeblock[#:lang "bjolang"]|{
(import/extern
(read-text-async (: System.IO.File.ReadAllTextAsync (-> string string) #:async)))
(defbjo (main)
(println (string-length (read-text-async "deck.bjo")))
0)
}|
A method that blocks its thread should be marked @code{#:blocking}. You then get a warning when you call it from a bjoroutine, and the fix is to call it through @code{(sync (blocking (fun () ...)))}, as in the concurrency section.
@subsubsection{Generics}
A generic .NET type is imported with type variables, and a generic method needs a signature that says what they are:
@codeblock[#:lang "bjolang"]|{
(import/class
((Dict %k %v) (: System.Collections.Generic.Dictionary)))
(import/extern
(dict-try-get (: System.Collections.Generic.Dictionary.TryGetValue
(-> (Dict %k %v) %k (out %v) (Option %v)))))
}|
Between the two worlds, a @code{Func<A, B>} is a @code{(-> %a %b)}, an @code{IEnumerable<T>} is a @code{(Seq %a)}, a C# tuple is a @code{Tuple} and @code{T[]} is an @code{(Array %a)}. Constructing a generic .NET class is not supported yet, and neither is matching on .NET objects with constructor patterns.
@subsubsection{Culture}
A bjolang program runs in the invariant culture, so a double is always written @code{2.5}, even on a Swedish machine where .NET would write @code{2,5}. If you want the user's own formatting, set @code{CultureInfo.CurrentCulture} yourself.
@subsubsection{Calling bjolang from C#}
A module compiles to a static class called @code{<module>_Module}, with a static method for each @code{defun} and a static field for each @code{def}, so C# can call it. Names that are not valid C# are mangled (@code{full-deck} becomes @code{fullsubdeck}), and the types are bjolang's own collections. @code{bjo build -d} writes the generated C# to @code{out.cs} if you want to see what you would be calling.
@subsection{Macros}
A macro is a function that the compiler runs while it reads your program. It gets the form you wrote, as data, and answers the form to use instead. Much of what looks like syntax in bjolang, @code{cond}, @code{when}, @code{type/derive} and @code{with-test} among others, is macros from the prelude and the standard library.
@subsubsection{def/macro}
A macro is defined with @code{def/macro}, takes three arguments, @code{form}, @code{inject} and @code{compare}, and is almost always written with @code{syntax-match} from @code{(std syntax-match)}:
@codeblock[#:lang "bjolang"]|{
(import (std syntax-match))
;; (with-each-card (c deck) body ...) runs the body once for every card.
(def/macro (with-each-card form inject compare)
(syntax-match form
((_ (name deck) body ...)
#'(vec-for-each (fun (,name) ,@body) ,deck))
(bad (syntax-error bad "(with-each-card (name deck) body ...)"))))
}|
@code{syntax-match} takes the form apart by shape. @code{_} matches anything (here the macro's own name), a name binds what it matches, and @code{...} after a pattern matches the rest. @code{#'(...)} builds the answer: a template where @code{,x} puts in a piece of syntax, and @code|{,@xs}| splices in a list of them. @code{syntax-error} rejects the input with a message pointing at where the macro was used.
A pattern like @code{((name value) ...)} matches any number of pairs and binds @code{name} and @code{value} to lists, one element per pair. A quoted name, @code{'else}, matches that identifier and nothing else.
A macro cannot be used in the module that defines it. It is compiled code that the compiler runs, so it has to be compiled before the code that uses it is read:
@codeblock[#:lang "text"]|{
'twice' is a macro defined in this module, and a macro cannot be used where it is defined,
at bad2.bjo:5. Its transformer runs inside the compiler, so it has to be compiled before
whatever uses it is read — which cannot be true of the file it is written in. Move it to a
module of its own and import that. An (include ...) will not do: an included file becomes
part of this one.
}|
So the macros of the card game go in @code{cardmacros.bjo}, and @code{(import "cardmacros.bjo")} is enough to use them. Macros need no export: every macro in a module arrives with it.
The input to a macro is a value of the type @code{Syntax}, a union with cases like @code{(SSym s)} for an identifier, @code{(SInt text)} for a number and @code{(SList items)} for a parenthesized form. A macro is an ordinary bjolang function, so it can call other functions, match on the @code{Syntax} directly, and recurse.
@subsubsection{Hygiene and inject}
Names that a template introduces are renamed so that they cannot clash with the names at the place the macro is used:
@codeblock[#:lang "bjolang"]|{
;; (or-else a b) is a, unless a is 0.
(def/macro (or-else form inject compare)
(syntax-match form
((_ a b) #'(let ((tmp ,a)) (if (= tmp 0) ,b tmp)))))
;; in another module
(def tmp 9)
(or-else 0 tmp) ;; 9, not 0
}|
The @code{tmp} in the template and the @code{tmp} the caller passed are two different variables, so the answer is the caller's 9. Names the template uses without binding them, like @code{let}, @code{if} and @code{=} here, still mean what they mean in the prelude, even if the calling module has something else by that name.
Sometimes you do want the macro to bind a name the caller can see. @code{(inject 'name)} makes an identifier that is not renamed:
@codeblock[#:lang "bjolang"]|{
;; (aif test then else): in `then`, `it` is what was inside the Some.
(def/macro (aif form inject compare)
(syntax-match form
((_ test then else)
#'(match ,test
((Some ,(inject 'it)) ,then)
(None ,else)))))
(aif (map-try-ref #map(("a" 1)) "a")
(println #"found ${it}")
(println "nothing"))
}|
@subsubsection{Several forms: begin}
A macro answers one form. To define several things, answer a @code{(begin ...)}, whose contents are spliced in where the macro was used:
@codeblock[#:lang "bjolang"]|{
;; (def/counter name) defines a function that counts how often it is called.
(def/macro (def/counter form inject compare)
(syntax-match form
((_ name)
#'(begin
(def/mutable count 0)
(: ,name (-> int))
(defun (,name)
(set! count (+ count 1))
count)))))
;; in another module
(def/counter next-id)
(next-id) ;; 1
(next-id) ;; 2
}|
@code{count} is written in the template, so it is renamed like everything else, and a @code{count} in the calling module is not disturbed. @code{next-id} came from the caller, so it is defined under that name.
@subsubsection{Pattern macros: def/pattern}
@code{def/pattern} defines a macro that is used in patterns instead of expressions. A pattern macro without arguments can be written as a bare capitalized name:
@codeblock[#:lang "bjolang"]|{
;; Face matches a jack, a queen or a king.
(def/pattern (Face form inject compare)
(syntax-match form
(_ #'(or Jack Queen King))))
;; in another module
(: court? (-> Card bool))
(defun (court? c)
(match (record-ref c rank)
(Face #t)
(_ #f)))
}|
Pattern macros are expanded before the exhaustiveness check, so the compiler sees the @code{or} and checks it like one you wrote yourself.
@subsubsection{Hash macros: def/hash-extend}
@code{#map(...)} is a hash macro, and you can write your own with @code{def/hash-extend}. Here is a card literal. The rank and suit are turned into constructors by two helper functions:
@codeblock[#:lang "bjolang"]|{
(import (std syntax-match) "deck.bjo")
(re-export Card Rank Suit)
;; A number is a Pip, a letter a court card.
(: rank-syntax (-> Syntax Syntax))
(defun (rank-syntax r)
(syntax-match r
('J #'Jack) ('Q #'Queen) ('K #'King) ('A #'Ace)
(n (match n
((SInt _) #'(Pip ,n))
(_ (syntax-error n "a rank is 2 to 10, J, Q, K or A"))))))
(: suit-syntax (-> Syntax Syntax))
(defun (suit-syntax s)
(syntax-match s
('clubs #'Clubs) ('diamonds #'Diamonds) ('hearts #'Hearts) ('spades #'Spades)
(bad (syntax-error bad "a suit is clubs, diamonds, hearts or spades"))))
;; #card(Q hearts) is (Card (rank Queen) (suit Hearts)).
(def/hash-extend (card form inject compare)
(syntax-match form
((_ r s) #'(Card (rank ,(rank-syntax r)) (suit ,(suit-syntax s))))
(bad (syntax-error bad "#card takes a rank and a suit: #card(Q hearts)"))))
}|
@codeblock[#:lang "bjolang"]|{
(import "deck.bjo" "cardmacros.bjo")
(defun (main)
(println #card(Q hearts)) ;; Q♥
(println #card[10 spades]) ;; 10♠, either bracket works
(println (court? #card(K clubs))) ;; True
(with-each-card (c [#card(A spades) #card(2 hearts)])
(println #"a card: ${c}"))
0)
}|
A mistake is reported where the literal is written:
@codeblock[#:lang "text"]|{
The hash macro 'card' failed at bad.bjo:3: a suit is clubs, diamonds, hearts or spades — in hurts
}|
The @code{re-export} in @code{cardmacros.bjo} is there because the template writes @code{Card}, @code{Queen} and the rest, and those have to mean something in the module where the macro is used. With the re-export, importing @code{cardmacros.bjo} is enough to make them resolve. It is still a good idea to import @code{deck.bjo} as well, since that is where the @code{->str} implementations live.
@subsubsection{Literals that elaborate into your unions}
Many small languages do not need a macro at all. Where a union is expected, a quoted list is turned into the union's cases, and a case can declare with @code{#:tag} the name it is written under:
@codeblock[#:lang "bjolang"]|{
(type (: Action (Union (: Play int #:tag play)
(: Say string #:tag say)
(: Pass #:tag pass))))
(: script (List Action))
(def script '((play 3) (say "your turn") pass (play 1)))
;; = (list (Play 3) (Say "your turn") Pass (Play 1))
(def n 7)
(def (: more (List Action)) '((play ,n) pass))
}|
This is checked by the type checker, not by a macro, so a wrong tag or the wrong number of arguments is a type error:
@codeblock[#:lang "text"]|{
Type Error at elab2.bjo:5: `sya` is not a tag of the union elab2/Action and no case of it
carries a list, so this literal cannot be one. Its tags are play, say or pass.
}|
It only works where the type checker knows a union is expected, which is why @code{script} and @code{more} have types. @code{(std run)}'s commands and @code{(std fmt)}'s layouts are both written this way.
@subsubsection{What a macro cannot do}
@read-list{
- Be used in the module that defines it.
- Be used anywhere but where an expression or a definition goes (or a pattern, for @code{def/pattern}). Not as a @code{loop} clause, a @code{let} binding or a type.
- Replace a special form. A macro called @code{if} is never used.
- See types. Macros are expanded before type checking.
- Be passed around as a value.
- Add an import. Write the import in the file that uses the macro.
}
@subsection{Testing and the REPL}
@subsubsection{Tests with (std simpletest)}
@code{(std simpletest)} is a small test library. @code{(expect label actual wanted)} compares two values, @code{(expect-true label ok)} checks a boolean, and @code{(fail label)} marks a branch that should not be reached. Each prints @code{ok: label} or a line starting with @code{FAILURE:}:
@codeblock[#:lang "bjolang"]|{
;; tests/deck-test.bjo
(import (std simpletest) (cardlib deck))
(defun (main)
(expect "a deck has 52 cards" (vec-length (full-deck)) 52)
(expect "an ace is worth 11" (card-points (Card (rank Ace) (suit Spades))) 11)
(expect-true "shuffling keeps the cards" (= (vec-length (shuffle (full-deck))) 52))
0)
}|
@codeblock[#:lang "text"]|{
$ bjo run tests/deck-test.bjo
ok: a deck has 52 cards
ok: an ace is worth 11
ok: shuffling keeps the cards
}|
A failure shows both values: @code{FAILURE: deliberately wrong gave 2 but wanted 3}.
@code{(with-test "name" body ...)} runs a test body in a scope with a deadline. The test gets a timeout that names it, fibers it spawned are waited for, and it fails if it left a file open. Since it is a scope, it has to be in a bjoroutine.
@code{(with-fake-fs ((path contents) ...) body ...)} replaces the file system with a map for everything inside it. Here is a test of the high-score code from the I/O section, with the score functions moved to a module of their own. The real @code{scores.txt} is not touched:
@codeblock[#:lang "bjolang"]|{
(import (std simpletest) (std effect) "scores.bjo")
(defbjo (main)
(with-test "no file means no scores"
(with-fake-fs ()
(expect "empty" (read-scores) [])))
(with-test "a bad line is skipped"
(with-fake-fs (("scores.txt" "ada 31\nnonsense\nbo 27\n"))
(expect "two scores" (read-scores) [(Tuple "ada" 31) (Tuple "bo" 27)])))
(with-test "a saved score reads back"
(with-fake-fs (("scores.txt" "ada 31\n"))
(save-score "cy" 12)
(expect "appended" (vec-length (read-scores)) 2)))
0)
}|
@code{with-fake-fs} is written with @code{with-handler}, so the test has to import @code{(std effect)} as well. Tests go in @code{tests/} in a project, and are run with @code{bjo run}.
@subsubsection{The REPL}
@code{bjo repl} starts the REPL (inside a project, the project's modules can be imported). It has no line editing of its own, so if @code{rlwrap} is installed, @code{bjo} uses it. An entry is either one expression, whose value is printed, or a group of definitions, which prints the names it defined:
@codeblock[#:lang "text"]|{
bjo> (+ 1 2)
3
bjo> (import "deck.bjo")
bjo> (vec-slice (full-deck) 0 3)
[2♣ 3♣ 4♣]
bjo> (defun (double (: x int)) : int (* x 2))
double
bjo> (double 21)
42
}|
The REPL is not an interpreter. Every entry is compiled by the same compiler as a file, into a small assembly of its own, so it behaves exactly like the same code in a file would.
A top-level @code{defun} needs a signature at the prompt too. Either write the types on the definition, as with @code{double} above, or type the signature on its own line first, and the next entry picks it up:
@codeblock[#:lang "text"]|{
bjo> (: triple (-> int int))
bjo> (defun (triple x) (* x 3))
triple
}|
Redefining a name shadows the old one, but code that was already compiled against the old one keeps calling it:
@codeblock[#:lang "text"]|{
bjo> (defun (f (: x int)) : int (+ x 1))
f
bjo> (defun (g (: x int)) : int (f (f x)))
g
bjo> (defun (f (: x int)) : int (* x 100))
note: f shadows the one from entry 1. Anything already compiled against that one still calls it.
f
bjo> (g 10)
12
}|
The same goes for @code{impl}s. An implementation typed at the prompt is used by the entries after it, not by the ones before. @code{:help} lists the commands, and @code{:quit} or Ctrl-D leaves.