Add a refactoring between fun x -> x.Prop and _.Prop - #20535
xperiandri wants to merge 3 commits into
Conversation
❗ Release notes requiredYou can open this PR in browser to add release notes: open in github.dev
|
ee5d78c to
119ccbf
Compare
fun x -> x.Prop and the _.Prop shorthand
A lambda whose only use of its parameter is the head of a member, method, indexer or type-application chain converts to the F# 8 shorthand, and a shorthand converts back to a 'fun' lambda with a parameter name the body does not use. The accepted chains mirror SyntaxTreeOps.pushUnaryArg, so the shorthand produced always type checks like the lambda it replaces. A lambda spanning multiple lines still converts as long as its body is such a chain; the result collapses onto the line the '_.' replaces 'fun' on. Fixes dotnet#16234 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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>
f1fdbc0 to
0bc4d75
Compare
|
🔍 Tooling Safety Check — Affects-Design-Time
|
| String.Equals(ident.idText, name, StringComparison.Ordinal) | ||
|
|
||
| // The shapes SyntaxTreeOps.pushUnaryArg accepts under `_.`, with the parameter as the head of the chain. | ||
| let rec tryChainRoot expr = |
There was a problem hiding this comment.
Is there no way to expose this logic from F.C.Service as an API ? (and reuse/parametrize/abstract with existing logic there is inside)
This duplication has potential to drift if compiler's logic changes.
| | SyntaxNode.SynExpr(SynExpr.Paren(expr = inner; leftParenRange = leftParen; rightParenRange = Some rightParen; range = parenRange) as paren) :: SyntaxNode.SynExpr(SynExpr.App( | ||
| flag = ExprAtomicFlag.NonAtomic; isInfix = false; argExpr = arg)) :: _ when | ||
| obj.ReferenceEquals(inner, lambda) | ||
| && obj.ReferenceEquals(arg, paren) | ||
| && isBlankBetween sourceText (spanOf sourceText leftParen).End lambdaSpan.Start | ||
| && isBlankBetween sourceText lambdaSpan.End (spanOf sourceText rightParen).Start |
There was a problem hiding this comment.
Extract, e.g. to a well named AP where the AP will explain what this attempts to check.
T-Gro
left a comment
There was a problem hiding this comment.
Maybe in general, could you run e.g. 2-3 rounds of the "code compaction" skill from this repo which will check any shared patterns between added code and code which already exists in the compiler (like various Ops, helpers or other pieces of logic) - the skill covers some common patterns of logic reuse.
fun x -> x.Prop and the _.Prop shorthandfun x -> x.Prop and _.Prop
Covered casesConverts
Not offered
|
Fixes #16234
Offers
Ctrl+.to turn a lambda that only reads a member chain of its parameter (fun x -> x.Name,fun x -> x.M(y),fun x -> x.Items[0]) into the_.shorthand, and a shorthand back into afunlambda. It is a refactoring rather than a code fix because both forms are correct.Not offered for lambdas with several or annotated parameters, bodies that do more than read the chain, lambdas applied directly, or inside quotations.
🤖 Generated with Claude Code