Repository navigation
Category Match
Home › All Categories › Match
match l ' a statement: 'case' arms, any body
case Red : Stop( )
case Amber : SlowDown( )
case Green : Go( )
end match
dim as ustring label = match code ' a value: 'pattern => value' arms
200 => "OK"
301, 302 => "Redirect"
400 to 499 => "Client error"
else => "Unknown"
end matchselect case does not check that every case is handled, cannot produce a value, and cannot take
a tuple or a UDT apart. Add a member to an enum and every select case that forgot it still
compiles. match refuses to compile until every value of its subject is handled, and it binds the
parts of the value it matched.
select case is unchanged, and stays the branch that does not have to be exhaustive.
| Statement | Value | |
|---|---|---|
| Where | at the start of a statement | anywhere a value is expected |
| Arm |
case pattern [if guard], then a body of any length |
pattern [if guard] => value, one line |
| Catch-all | case else |
else => value |
case arms in a match used as a value are TKO1242; => arms in a match statement are TKO1243.
else must be the last arm (TKO1244) and takes no guard (TKO1249).
| Pattern | Matches | Binds |
|---|---|---|
v |
the subject = v (any expression; strings compare as strings) |
— |
a to b |
a <= subject <= b |
— |
is <relop> v |
subject <relop> v |
— |
p1, p2 |
either (top level only) | nothing (TKO1248) |
any |
anything | — |
( p1, p2 ) |
a tuple, element by element | what the elements bind |
Name( p1, p2 ) |
a UDT of type Name, field by field in declaration order |
what the fields bind |
x as Circle |
a Shape ptr that is not null and points at a Circle
|
x, a Circle ptr
|
any as Circle |
the same, binding nothing | — |
starts "pre" rest |
a string that begins with "pre"
|
rest, the remainder |
match p
case Point( 0, 0 ) : print "origin"
case Point( x, y ) if x = y : print "diagonal"; x
case Point( x, y ) : print x; ","; y
end match
dim as double area = match shape ' shape as Shape ptr
c as Circle => 3.14159 * c->r ^ 2
r as Rect => r->w * r->h
else => 0
end match
match ( isAdmin, isOwner )
case ( true, any ) : Allow( )
case ( false, true ) : AllowOwnOnly( )
case ( false, false ) : Deny( )
end match
match cmd
case "quit", "exit" : running = false
case starts "load " rest : Load( rest )
case else : print "unknown: "; cmd
end matchAt the top level a bare name is a value, as in select case: case Red compares. Inside a tuple
or UDT pattern a bare name binds: Point( x, 0 ) binds x. To compare against a constant there,
qualify it (Point( Limits.MaxX, y )) or use a guard. true, false and nullptr are always values.
Name( ... ) is always a UDT pattern. To compare against what a call returns, write is = F( x ).
pattern if cond matches only when cond holds; a failed guard tries the next arm. The names the
pattern binds are visible in the guard. A guarded arm counts for nothing in the exhaustiveness check.
A match that leaves a value unhandled is TKO1250, and the message names one:
error TKO1250: 'match' is not exhaustive: green is not handled
error TKO1250: 'match' is not exhaustive: (FALSE, FALSE) is not handled
error TKO1250: 'match' is not exhaustive: any other value is not handled
-
boolean:
trueandfalse. -
a named enum: its members' values (two members with one value are one case). An anonymous enum
needs
else. - a tuple or UDT: every combination of its elements' or fields' cases.
-
everything else — integers, floats, strings, pointers, type patterns: only
else,anyor a binding covers it.
A value pattern that is not true, false, an enum member or an integer equal to one is matched at
run time but covers nothing. An arm the arms above it already cover is warning TKO1251.
- The subject is evaluated once, into a copy. Bindings alias that copy: writing to one does not
change the original. (
starts' remainder is its own string.) - Arms are tried top to bottom; the first whose pattern and guard both hold runs. There is no
fallthrough and no
exit match. -
A
matchused as a value evaluates its subject before the statement that contains it. That is invisible unless the subject has side effects and sits where it would not otherwise have run:x andalso match F( ) ...callsFwhateverxis. For the same reason amatchexpression is refused (TKO1245) as awhile/do/loopcondition and in a declaration. - An enum variable can hold a value no member has. The check cannot see that: a
matchstatement runs no arm for it, and amatchused as a value gives its last arm's value.
match is the keyword only when a subject follows it to the end of the line, the next line is an arm,
and an end match closes it. Everywhere else it is the name it is in FreeBASIC: dim match,
match = 3, sub match, match( 21 ), a label match:, and end match as END with an exit code.
A paren-less call match 10 inside a select case arm keeps its meaning too: the end select comes
before any end match.
| Id | |
|---|---|
| TKO1240 | a match with no arms |
| TKO1241 | no end match before the end of the procedure or file |
| TKO1242 / 1243 | the other form's arms |
| TKO1244 |
else that is not the last arm |
| TKO1245 | a match expression as a loop condition or in a declaration |
| TKO1246 | a malformed pattern |
| TKO1247 | a name bound twice in one pattern |
| TKO1248 | a pattern list that binds |
| TKO1249 | a guard on else
|
| TKO1250 | not exhaustive |
| TKO1251 | an arm that can never run (warning) |
| TKO1252 |
Name( ... ) over a subject of another type |
| TKO1253 | the wrong number of fields |
| TKO1254 | a type pattern over something that is not a pointer to a class |
| TKO1255 |
starts over something that is not a string |
| TKO1256 | a binding in a match used as a value that hides a constant (warning) |
| TKO1257 | a tuple pattern over something that is not a tuple |
- A pattern that does not fit its subject also draws the errors of the code it is rewritten into
(
'mid' takes a stringbeside TKO1255, for instance). - No
matchexpression in anelseifcondition or acasevalue. - No
exit match. - Sum types, which
matchwill take apart when they exist, are a separate step.
src/parser/match.tiko rewrites a match in the token buffer, before the parser reads it, into
scope, var, if and iif — as property get/set blocks are rewritten. A UDT's kth field is
reached through __tko_pos<k>, which Check's member lookup answers. What needs types is left as a
record on the AST arena and judged by src/symbols/semmatch.tiko.
tests/codegen/match-patterns-run, match-eval-run, match-contextual-run,
match-contextual-var-run; tests/diagnostics/match-shape, match-types, match-unclosed.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Match
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Array Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- tiko test
- tiko doc
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing