Skip to content
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

Support [<CallerArgumentExpression>] #966

Open
5 tasks done
Happypig375 opened this issue Jan 21, 2021 · 7 comments
Open
5 tasks done

Support [<CallerArgumentExpression>] #966

Happypig375 opened this issue Jan 21, 2021 · 7 comments

Comments

@Happypig375
Copy link
Contributor

Happypig375 commented Jan 21, 2021

Champion "CallerArgumentExpression" is currently in the C# team's working set.

I propose we support this attribute, which has been present since .NET Core 3.0. We can match this attribute by namespace so that it can be defined even when outside .NET Core.

The motivation, pros, and cons can be seen in the C# proposal. To summarize, this allows developers to capture the expressions passed to a method, to enable better error messages in diagnostic/testing APIs and reduce keystrokes.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M

Related suggestions: #463 - F# compiler should support CallerLineNumber, CallerFilePath etc (Completed for F# 4.1)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

@dsyme
Copy link
Collaborator

dsyme commented Jan 21, 2021

Note F# does let you use ReflectedDefinition attribute on method parameters, e.g.

type B() =
    static member M([<ReflectedDefinition>] x: Quotations.Expr<int>) = printfn "x = %A" x

B.M(1+1)

gives

x = Call (None, op_Addition, [Value (1), Value (1)])

You can also add "WithValue" which both evaluates the expression and gives its quotation:

type B() =
    static member M([<ReflectedDefinition(includeValue=true)>] x: Quotations.Expr<int>) = printfn "x = %A" x

B.M(1+1)

gives

x = WithValue (2, Call (None, op_Addition, [Value (1), Value (1)]))

For example you can get both a closure and the quotation of the closure.

@Happypig375
Copy link
Contributor Author

But that is not searchable in the codebase and is too verbose for debugging purposes.

@dsyme
Copy link
Collaborator

dsyme commented Jan 21, 2021

But that is not searchable in the codebase and is too verbose for debugging purposes.

Yes, capturing the string has different properties

@Happypig375
Copy link
Contributor Author

This will be in C# vNext: dotnet/roslyn#51952

@abelbraaksma
Copy link
Member

abelbraaksma commented May 16, 2022

It has since gotten into C# 10. Here's some docs and use cases.

PS: Championing issue is still open only because ECMA Spec isn't written yet.

@dsyme
Copy link
Collaborator

dsyme commented May 16, 2022

Marked as approved-in-principle

@ijklam
Copy link

ijklam commented Nov 24, 2023

How can we implement the feature? At present, the compiler does not store the source code during compilation (or maybe I didn't find out), so it cannot obtain the original source text of the argument...

So there might be two way to make it.

One is to read the source text through the FileSystem, by this way we can gain the original texts, but may be unusable in the fsi (since the filename is default to "stdin").

The other is to convert the expressions of the argument into code text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants