Skip to content

Commit e9cba47

Browse files
Copilotgewarren
andcommitted
Consolidate F# optional parameters documentation
Co-authored-by: gewarren <[email protected]>
1 parent e7b1595 commit e9cba47

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

docs/fsharp/language-reference/members/methods.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,7 @@ Overloaded methods are methods that have identical names in a given type but tha
9595

9696
## Optional Arguments
9797

98-
Starting with F# 4.1, you can also have optional arguments with a default parameter value in methods. This is to help facilitate interoperation with C# code. The following example demonstrates the syntax:
99-
100-
```fsharp
101-
open System.Runtime.InteropServices
102-
// A class with a method M, which takes in an optional integer argument.
103-
type C() =
104-
member _.M([<Optional; DefaultParameterValue(12)>] i) = i + 1
105-
```
106-
107-
Note that the value passed in for `DefaultParameterValue` must match the input type. In the above sample, it is an `int`. Attempting to pass a non-integer value into `DefaultParameterValue` would result in a compile error.
98+
F# supports optional arguments for methods. For detailed information about the different forms of optional arguments available in F#, see [Optional Parameters](../parameters-and-arguments.md#optional-parameters).
10899

109100
## Example: Properties and Methods
110101

docs/fsharp/language-reference/parameters-and-arguments.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ Note that those members could perform any arbitrary work, the syntax is effectiv
124124

125125
## Optional Parameters
126126

127+
F# supports two distinct forms of optional parameters for methods, each serving different purposes:
128+
129+
### Optional Arguments with Late Defaults (F# Native)
130+
127131
You can specify an optional parameter for a method by using a question mark in front of the parameter name. From the callee's perspective, optional parameters are interpreted as the F# option type, so you can query them in the regular way that option types are queried, by using a `match` expression with `Some` and `None`. Optional parameters are permitted only on members, not on functions created by using `let` bindings.
128132

129133
You can pass existing optional values to method by parameter name, such as `?arg=None` or `?arg=Some(3)` or `?arg=arg`. This can be useful when building a method that passes optional arguments to another method.
@@ -145,7 +149,9 @@ Baud Rate: 9600 Duplex: Full Parity: false
145149
Baud Rate: 4800 Duplex: Half Parity: false
146150
```
147151

148-
For the purposes of C# and Visual Basic interop you can use the attributes `[<Optional; DefaultParameterValue<(...)>]` in F#, so that callers will see an argument as optional. This is equivalent to defining the argument as optional in C# as in `MyMethod(int i = 3)`.
152+
### Optional Arguments with Early Defaults (C# Interop)
153+
154+
For the purposes of C# and Visual Basic interop you can use the attributes `[<Optional; DefaultParameterValue<(...)>]` in F#, so that callers will see an argument as optional. This is equivalent to defining the argument as optional in C# as in `MyMethod(int i = 3)`. This form was introduced in F# 4.1 to help facilitate interoperation with C# code.
149155

150156
```fsharp
151157
open System

0 commit comments

Comments
 (0)