You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp/language-reference/pattern-matching.md
+61-3Lines changed: 61 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -185,15 +185,30 @@ The following code shows some additional uses of the wildcard pattern:
185
185
186
186
## Patterns That Have Type Annotations
187
187
188
-
Patterns can have type annotations. These behave like other type annotations and guide inference like other type annotations. Parentheses are required around type annotations in patterns. The following code shows a pattern that has a type annotation.
188
+
Patterns can have type annotations. These behave like other type annotations and guide inference like other type annotations. Parentheses are required around type annotations in patterns.
189
+
190
+
A pattern with a type annotation uses the syntax `pattern : type` and provides **compile-time type information** to the type checker. This is purely a static type annotation that helps with type inference - it doesn't perform any runtime type checking or conversion. The compiler uses this information during compilation to determine the type of the pattern variable.
191
+
192
+
The following code shows a pattern that has a type annotation:
In this example, `(var1 : int)` tells the compiler that `var1` is of type `int`. This is resolved at compile time, and the generated code treats `var1` as an integer throughout the match expression. This pattern will match any integer value and bind it to `var1`.
197
+
198
+
**Key characteristics:**
199
+
200
+
- Uses the syntax `pattern : type` (with a single colon).
201
+
- Resolved at **compile time** - provides type information to the type checker.
202
+
- Does not perform runtime type testing.
203
+
- Used for type inference and to guide the compiler.
204
+
192
205
## Type Test Pattern
193
206
194
-
The type test pattern is used to match the input against a type. If the input type is a match to (or a derived type of) the type specified in the pattern, the match succeeds.
207
+
The type test pattern is used to match the input against a type**at runtime**. If the input type is a match to (or a derived type of) the type specified in the pattern, the match succeeds.
195
208
196
-
The following example demonstrates the type test pattern.
209
+
A type test pattern uses the syntax `:? type` and performs **runtime type checking**, similar to the `is` or `as` operators in C#. This pattern tests whether a value is of a specific type during program execution, making it useful when working with inheritance hierarchies or interface implementations.
210
+
211
+
The following example demonstrates the type test pattern:
|**Syntax**| Single colon: `a : int`| Colon with question mark: `:? Button`|
244
+
|**When resolved**| Compile time | Runtime |
245
+
|**Purpose**| Guides type inference | Tests actual type of value |
246
+
|**Use case**| Helping the compiler understand types | Checking runtime types in inheritance hierarchies |
247
+
|**Equivalent in C#**| Type annotations in switch patterns |`is` or `as` operators |
248
+
249
+
The following example demonstrates the differences:
250
+
251
+
```fsharp
252
+
// Type annotation pattern - compile time
253
+
let detect1 x =
254
+
match x with
255
+
| 1 -> printfn "Found a 1!"
256
+
| (var1 : int) -> printfn "%d" var1
257
+
// The ': int' tells the compiler var1 is an int
258
+
// Everything is resolved at compile time
259
+
260
+
// Type test pattern - runtime
261
+
type A() = class end
262
+
type B() = inherit A()
263
+
264
+
let test (a: A) =
265
+
match a with
266
+
| :? B -> printfn "Runtime check: it's a B"
267
+
| _ -> printfn "Runtime check: it's not a B"
268
+
// The ':? B' performs a runtime type check
269
+
// The actual type is tested during execution
270
+
```
271
+
214
272
## Null Pattern
215
273
216
274
The null pattern matches the null value that can appear when you are working with types that allow a null value. Null patterns are frequently used when interoperating with .NET Framework code. For example, the return value of a .NET API might be the input to a `match` expression. You can control program flow based on whether the return value is null, and also on other characteristics of the returned value. You can use the null pattern to prevent null values from propagating to the rest of your program.
0 commit comments