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.