A language with typesetting built in
СсылкаTopaz is a general purpose language with typesetting built in. This nonnormative tour connects its main ideas through a complete program. The linked reference chapters define the rules.
Bindings
СсылкаBindings name values and introduce names into lexical scopes. Use let for immutable bindings, let mut for reassignment, and const for constant definitions.
Types
СсылкаTypes describe values and guide inference from annotations and surrounding expressions. Records, enums and newtypes give data a named shape, alternatives, or a distinct identity.
Functions and abstraction
СсылкаFunctions and generics define reusable calculations with parameter and result types. Functions are values, and lambdas provide short function expressions. Protocols and implementations name capabilities that generic code can require.
Execution and control flow
СсылкаExpressions and operators compute values, while blocks and control flow sequence work and repeat effects. Pattern matching selects code by the shape of a value. Collections and comprehensions collect values from iteration.
Determinism
СсылкаLanguage determinism concerns the order of evaluation and the values a program computes from its inputs. Typesetting determinism concerns the layout that program produces. For the same program and text, the same font data, measurements, Unicode version, policies, grid, and product release produce the same typesetting result. Font data and measurements are program inputs.
Failure handling
СсылкаFailure and propagation separates expected errors in Result from runtime faults. Ok(value) carries success, Err(error) carries failure, and ? forwards an error from the enclosing function.
Concurrency
СсылкаConcurrency runs named arms as a group and collects their values in a record. A Duration deadline gives the group a timeout and an else path.
Modules
СсылкаModules split a program into files and expose selected declarations through export and import. Imported declarations retain their defining identity, whether selected by name or reached through a module namespace.
Libraries
СсылкаThe library provides bytes, data formats, files, and standard input and output. Typesetting supplies measured lengths, text break classification, and paragraph plans. Lispex evaluates packaged rules with explicit inputs and limits.
A complete program
СсылкаSave the program as language-. It prints each valid job and reports an error for a job with no copies.
Print jobs and handle an error
Ссылкаimport std.iorecord Job { text: string, copies: int }function checkedCopies(job: Job) -> Result<int, string> {if (job.copies > 0) {Ok(job.copies)} else {Err("copies must be positive")}}function printJob(job: Job) -> Result<int, string> {let copies: int = checkedCopies(job)?for copy in (0 ..< copies) {io.writeLine(job.text)}Ok(copies)}let jobs: Array<Job> = [Job { text: "Topaz", copies: 2 },Job { text: "Draft", copies: 0 },]for job in (jobs) {match (printJob(job)) {case Ok(copies) => io.writeLine("printed {copies} copies")case Err(message) => io.writeLine(message)}}
Topaz
Topaz
printed 2 copies
copies must be positive
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
TopazTopazprinted 2 copiescopies must be positive
The first job reaches the loop and prints two lines. The second job returns an error through ?, so the final match prints its message.