Refactoring between F# and .NET-compatible optional parameters - #20547
Open
xperiandri wants to merge 4 commits into
Open
xperiandri wants to merge 4 commits into
xperiandri wants to merge 4 commits into
Conversation
…meters ?x: T is optional for F# callers and gives the body an option; [<Optional; DefaultParameterValue(c)>] x: T is optional for C# and VB callers too and gives the body the value. The refactoring moves the default between defaultArg x c in the body and the attribute, adds the InteropServices open when it is missing, and leaves call sites alone: F# accepts both forms the same way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
❗ Release notes requiredYou can open this PR in browser to add release notes: open in github.dev
|
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
3 tasks
…form [<Optional; DefaultParameterValue(c)>] x: T now converts back to either ?x: T with let x = defaultArg x c, or, from F# 10, [<Struct>] ?x: T with let x = defaultValueArg x c. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…e of its section Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
3 tasks
xperiandri
marked this pull request as ready for review
September 14, 2026 19:40
Contributor
|
🔍 Tooling Safety Check — Affects-Design-Time
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds an editor refactoring (
Ctrl+.on a member's optional parameter) that switches it between the F# form and the .NET-compatible form:⟷
?x: Tis optional only for F# callers;[<Optional; DefaultParameterValue(c)>] x: Tis optional for C# and VB callers as well. F# call sites need no change for either form (M(),M(x = 1)andM(?x = opt)all compile against both), so only the member itself is edited.To
[<Optional; DefaultParameterValue(c)>]. Every use of the parameter in the member body must bedefaultArg x cwith the same constantc, of a type that matches the parameter's annotation (so the attribute does not trigger FS3211). A line that only rebinds the parameter,let x = defaultArg x c, is deleted; any otherdefaultArg x cbecomesx. A parameter with no uses becomes[<Optional>] x: T.open System.Runtime.InteropServicesis added when the file does not have it.Back to an F# optional parameter. Two actions are offered:
Optional/DefaultParameterValueattribute lists are removed,?goes back in front of the name, andlet x = defaultArg x cbecomes the first line of the body;LanguageFeature.SupportValueOptionsAsOptionalParameters): the same, with[<Struct>] ?x: Tandlet x = defaultValueArg x c.A body written on the member's own line moves below it. Without
DefaultParameterValuethe default isUnchecked.defaultof<_>.Not offered for an unannotated
?x, a[<Struct>] ?x(towards the .NET form), uses of the parameter other thandefaultArg x c, differing or non-constant defaults, attribute lists that hold other attributes too, overrides, interface implementations, abstract/default slots, constructors, extension members, or a file with a paired.fsi.Covered cases
Converts
?greeting: stringwithlet greeting = defaultArg greeting "Hello"→[<Optional; DefaultParameterValue("Hello")>] greeting: string, theletline removed,open System.Runtime.InteropServicesinserted; callsGreet("Ada")andgreeting = "Hi"stay as they are?step: int+let step = defaultArg step 1⟷[<Optional; DefaultParameterValue(1)>] step: int; the same for a second parameter?step: floatwith0.5value + defaultArg step 1→[<Optional; DefaultParameterValue(1)>] step: int) = value + stepstep: int) = value + step→?step: int) =, thenlet step = defaultArg step 1andvalue + stepon their own lines?flag: bool(unused) →[<Optional>] flag: boolDefaultParameterValue:[<Optional>] step: int→?step: int+let step = defaultArg step Unchecked.defaultof<_>[<Optional; DefaultParameterValue(1)>] step: int→[<Struct>] ?step: int+let step = defaultValueArg step 1;[<Optional>] step: int→… defaultValueArg step Unchecked.defaultof<_>Use [<Optional; DefaultParameterValue>] for optional parameter; the .NET form offersUse F# '?' optional parameter, thenUse F# '[<Struct>] ?' optional parameter--langversion:9.0), the .NET form offers onlyUse F# '?' optional parameterNot offered
defaultArg x 0 + defaultArg x 1defaultArg:static member M(?x: int) = x.IsSome?x: objwithdefaultArg x (box 1)?x: intwithdefaultArg x 1.0static member M(?x) = defaultArg x 0[<Struct>] ?x: intwithdefaultValueArg x 0[<Optional; In>] x: intabstract M: ?x: int -> intwithdefault _.M(?x: int) = defaultArg x 0static member M(x: int) = xstatic member M: ?x: int -> intPart of a series of optional-parameter and struct refactorings: #20546 switches
?xand[<Struct>] ?x, #20545 does option ⟷ struct for partial active patterns.Checklist
🤖 Generated with Claude Code