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
+59-3Lines changed: 59 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,15 +184,29 @@ The following code shows some additional uses of the wildcard pattern:
184
184
185
185
## Patterns That Have Type Annotations
186
186
187
-
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.
187
+
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.
188
+
189
+
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.
190
+
191
+
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`.
196
+
197
+
**Key characteristics:**
198
+
- Uses the syntax `pattern : type` (with a single colon)
199
+
- Resolved at **compile time** - provides type information to the type checker
200
+
- Does not perform runtime type testing
201
+
- Used for type inference and to guide the compiler
202
+
191
203
## Type Test Pattern
192
204
193
-
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.
205
+
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.
206
+
207
+
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.
194
208
195
-
The following example demonstrates the type test pattern.
209
+
The following example demonstrates the type test pattern:
|**Syntax**| Single colon: `a : int`| Colon with question mark: `:? Button`|
241
+
|**When resolved**| Compile time | Runtime |
242
+
|**Purpose**| Guides type inference | Tests actual type of value |
243
+
|**Use case**| Helping the compiler understand types | Checking runtime types in inheritance hierarchies |
244
+
|**Equivalent in C#**| Type annotations in switch patterns |`is` or `as` operators |
245
+
246
+
**Example showing the difference:**
247
+
248
+
```fsharp
249
+
// Type annotation pattern - compile time
250
+
let detect1 x =
251
+
match x with
252
+
| 1 -> printfn "Found a 1!"
253
+
| (var1 : int) -> printfn "%d" var1
254
+
// The ': int' tells the compiler var1 is an int
255
+
// Everything is resolved at compile time
256
+
257
+
// Type test pattern - runtime
258
+
type A() = class end
259
+
type B() = inherit A()
260
+
261
+
let test (a: A) =
262
+
match a with
263
+
| :? B -> printfn "Runtime check: it's a B"
264
+
| _ -> printfn "Runtime check: it's not a B"
265
+
// The ':? B' performs a runtime type check
266
+
// The actual type is tested during execution
267
+
```
268
+
213
269
## Null Pattern
214
270
215
271
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