-
Notifications
You must be signed in to change notification settings - Fork 6k
Consolidate F# optional parameters documentation #47580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: gewarren <[email protected]>
- [Optional arguments with late defaults (F# native)](#optional-arguments-with-late-defaults-f-native) | ||
- [Optional arguments with early defaults (C# interop)](#optional-arguments-with-early-defaults-c-interop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this terminology used or explained anywhere else in the docs?
I understand from the implementation perspective what it means, but maybe puting the names late
/ early
exposes too much detail here? (without further explanation).
- [Optional arguments with late defaults (F# native)](#optional-arguments-with-late-defaults-f-native) | ||
- [Optional arguments with early defaults (C# interop)](#optional-arguments-with-early-defaults-c-interop) | ||
|
||
### Optional arguments with late defaults (F# native) | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also mention usage of defaultArg
and shadowing to convert an optional argument to a value.
Possibly also mention the advantage - unlike "C# style extensions", this allows authors of the method to tell if caller passed in a value or not.
The F# documentation for optional parameters was incomplete and fragmented across multiple files. The
methods.md
file only covered C# interop-style optional arguments using[<Optional; DefaultParameterValue>]
attributes, while missing the native F# optional parameter syntax using?
.This PR consolidates all optional parameter documentation into the
parameters-and-arguments.md
file with clear distinctions between the two forms:?
syntax for F#-idiomatic optional parameters that are interpreted as F# option types[<Optional; DefaultParameterValue>]
attributes for C# compatibility, introduced in F# 4.1The
methods.md
file now simply references the comprehensive documentation inparameters-and-arguments.md
, eliminating duplication and providing developers with a single authoritative source for all optional parameter information.This addresses the concern raised in the issue that the F# language's native optional parameter mechanism was not covered in the documentation, which was "very curious" given how commonly this technique is used.
Fixes #45813.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Internal previews