Pattern matching with match
and is
Say which of several cases a value is with match, test one
pattern with is, and let the checker hold a match to every case
of a union.
After reading this page, you will be able to write a
match that picks a value by the shape of another, test one shape with is, compare with a value in scope, write a chain of conditions, and read what the checker says when a match leaves a case out.match and is are TC39's pattern-matching proposal, written as its README has it. Ljós adds no syntax of its own: a script is TypeScript, and a match is compiled away into plain TypeScript before anything else reads it.A
match goes in the scripts of a QML file of Ljós's own API (bindings, handlers and functions), and in an .lts module. A .ts module stays plain TypeScript, so that stock TypeScript tools read it as they read any other; a match in one is the error ts-lts-syntax, and ljos migrate --write renames the file to .lts and rewrites every import that names it, qmldir entries too:An
.lts module is imported as a .ts one is, and values and types cross both ways. A name.ts beside a name.lts is the error module-clash on both: one module name is one file.The arms are tried in order, and the first whose pattern matches gives the value. A literal matches the same value, with no coercion:
1 does not match "1".On a
number or a string, a match needs a default:: there is always another value. On a union of literals, a plain enum or a union of records told apart by one key, leave default out, and the checker holds the match to every case. Adding a member to the union then makes every match that misses it an error, where a default would quietly take it.Leave out
when "large":, and the check says which case is not handled, written as the pattern that would cover it:An object pattern matches an object with those keys, each matching its own pattern, and
let names what it matches. An array pattern matches an array of exactly that length; ...let rest makes it "at least". or matches either side. A guard, and if (condition), holds the arm to a condition on what it named.This is the shape of a piece of work that is loading, done or failed:
There is no
default, so the checker holds the match to every status. The guarded arm covers nothing for it, since the checker cannot know when titles.length > 99; the arm after it matches every done record whole, and the last arm covers failed.A bare name in a pattern compares with the value it holds; it never binds. To bind, write
let. A name that is not in scope is an error whose message gives the let fix.A chain of conditions with nothing to take apart is a
match whose arms are guards alone. Guards cover nothing, so the checker asks for a default:, which takes the place of a last else.A button picks one look from whether it is enabled, pressed and hovered. The
match says that the looks exclude one another and which wins, in the order of its arms:A
match whose arms are calls stands as a statement, so it replaces a switch in a handler. With no default, a status added later is an error here rather than a case that falls through:x is pattern is the same test as a boolean. Where it is true, x is narrowed, and what the pattern binds is in scope in the block around it:Reading
body where the checker cannot prove the is was true, in the else or after the if, is the error ts-is-binding.Extractors, custom matchers and the proposal's possible future additions, such as relational patterns, are not in it; a predicate or a guard says what they would.















