Skip to content

Conversation

@forki
Copy link
Contributor

@forki forki commented Jan 14, 2015

This pull request was moved from https://visualfsharp.codeplex.com/SourceControl/network/forks/forki/fsharp/contribution/7698 (see discussion there)

https://fslang.uservoice.com/forums/245727-f-language/suggestions/5900625-add-nameof-operator-to-follow-c-vb-update suggests we implement a "nameof" operator which is something similar to the typeof operator.

This feature was "approved in principle" by Don.

Small outline:

  • I added a nameof to env.fs
  • In opt.fs we rewrite the typed AST to const string expressions with the name
  • In check.fs we raise compiler errors for all unknown cases
  • prim-types.fs shows we will throw if we couldn't inline the nameof operator
  • src/fsharp/FSharp.Core.Unittests/NameOfTests.fs contains samples for all the known cases

Open questions:

  • How can we write unittests for the cases we don't want to support?
    • I'd like to write a test which shows tha "nameof(6)" fails with compiler error
  • The C# spec for nameof shows cases that retrieve names for types. Do we want this too?
  • ...

Related information:

C#/VB spec for nameof: https://roslyn.codeplex.com/discussions/570551
Mads showing the nameof operator: http://channel9.msdn.com/Events/Visual-Studio/Connect-event-2014/116 (at 5:35)

Latkin: F# 4.0 triage – This PR is not being considered for F# 4.0, but will be left open for the time being to allow for community feedback.

@KevinRansom
Copy link
Contributor

Thanks for moving this over Steffen.

From: Steffen Forkmann [mailto:[email protected]]
Sent: Wednesday, January 14, 2015 12:47 AM
To: Microsoft/visualfsharp
Subject: [visualfsharp] Nameof operator (#13)

ThispPull request was moved from https://visualfsharp.codeplex.com/SourceControl/network/forks/forki/fsharp/contribution/7698 (see discussion there)

https://fslang.uservoice.com/forums/245727-f-language/suggestions/5900625-add-nameof-operator-to-follow-c-vb-update suggests we implement a "nameof" operator which is something similar to the typeof operator.

This feature was "approved in principle" by Don.

Small outline:

  • I added a nameof to env.fs
  • In opt.fs we rewrite the typed AST to const string expressions with the name
  • In check.fs we raise compiler errors for all unknown cases
  • prim-types.fs shows we will throw if we couldn't inline the nameof operator
  • src/fsharp/FSharp.Core.Unittests/NameOfTests.fs contains samples for all the known cases

Open questions:

  • How can we write unittests for the cases we don't want to support?
  • I'd like to write a test which shows tha "nameof(6)" fails with compiler error
  • The C# spec for nameof shows cases that retrieve names for types. Do we want this too?
  • ...

Related information:

C#/VB spec for nameof: https://roslyn.codeplex.com/discussions/570551
Mads showing the nameof operator: http://channel9.msdn.com/Events/Visual-Studio/Connect-event-2014/116 (at 5:35)

Latkin: F# 4.0 triage – This PR is not being considered for F# 4.0, but will be left open for the time being to allow for community feedback.


You can merge this Pull Request by running

git pull https://github.com/forki/visualfsharp nameof

Or view, comment on, or merge it at:

#13

Commit Summary

  • Implementing basic nameof scenarios
  • Use the same function for check.fs and opt.fs
  • Fix check.fs types
  • Add QA tests which show expected compile errors
  • Add more unit tests
  • Don't allow to use nameof on applied or partially applied functions
  • Add some tests for name lookup of operators
  • Don't allow to use nameof(1+2)
  • Dogfood the nameof operator in FSharp.Core
  • NameOf should not work on applied functions
  • Revert problematic FSharp.Core files
  • Show we can find the name of members that are defined below
  • Prefix error cases with E_
  • Show user-defined nameof wins.
  • Don' report get_ for properties in nameof
  • Show that nameof can't be used as a function
  • Show how we can use nameof in pattern matching
  • Use nameof in quotation
  • Add a generic function as nameof test
  • Show methods with get_ work in nameof
  • Better structure in nameof tests

File Changes

  • M src/fsharp/FSComp.txthttps://github.com/WIP: Nameof operator #13/files#diff-0 (1)
  • M src/fsharp/FSharp.Core.Unittests/FSharp.Core.Unittests.fsprojhttps://github.com/WIP: Nameof operator #13/files#diff-1 (1)
  • A src/fsharp/FSharp.Core.Unittests/NameOfTests.fshttps://github.com/WIP: Nameof operator #13/files#diff-2 (202)
  • M src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fshttps://github.com/WIP: Nameof operator #13/files#diff-3 (1)
  • M src/fsharp/FSharp.Core/Linq.fshttps://github.com/WIP: Nameof operator #13/files#diff-4 (2)
  • M src/fsharp/FSharp.Core/Query.fshttps://github.com/WIP: Nameof operator #13/files#diff-5 (6)
  • M src/fsharp/FSharp.Core/array.fshttps://github.com/WIP: Nameof operator #13/files#diff-6 (314)
  • M src/fsharp/FSharp.Core/array2.fshttps://github.com/WIP: Nameof operator #13/files#diff-7 (16)
  • M src/fsharp/FSharp.Core/array3.fshttps://github.com/WIP: Nameof operator #13/files#diff-8 (8)
  • M src/fsharp/FSharp.Core/math/n.fshttps://github.com/WIP: Nameof operator #13/files#diff-9 (2)
  • M src/fsharp/FSharp.Core/math/z.fshttps://github.com/WIP: Nameof operator #13/files#diff-10 (24)
  • M src/fsharp/FSharp.Core/prim-types.fshttps://github.com/WIP: Nameof operator #13/files#diff-11 (3)
  • M src/fsharp/FSharp.Core/prim-types.fsihttps://github.com/WIP: Nameof operator #13/files#diff-12 (4)
  • M src/fsharp/FSharp.Core/seq.fshttps://github.com/WIP: Nameof operator #13/files#diff-13 (238)
  • M src/fsharp/check.fshttps://github.com/WIP: Nameof operator #13/files#diff-14 (28)
  • M src/fsharp/check.fsihttps://github.com/WIP: Nameof operator #13/files#diff-15 (1)
  • M src/fsharp/env.fshttps://github.com/WIP: Nameof operator #13/files#diff-16 (5)
  • M src/fsharp/opt.fshttps://github.com/WIP: Nameof operator #13/files#diff-17 (15)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfAdditionExpr.fshttps://github.com/WIP: Nameof operator #13/files#diff-18 (7)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfAppliedFunction.fshttps://github.com/WIP: Nameof operator #13/files#diff-19 (8)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfAsAFunction.fshttps://github.com/WIP: Nameof operator #13/files#diff-20 (7)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfDictLookup.fshttps://github.com/WIP: Nameof operator #13/files#diff-21 (8)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfIntConst.fshttps://github.com/WIP: Nameof operator #13/files#diff-22 (7)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfIntegerAppliedFunction.fshttps://github.com/WIP: Nameof operator #13/files#diff-23 (8)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfParameterAppliedFunction.fshttps://github.com/WIP: Nameof operator #13/files#diff-24 (9)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfPartiallyAppliedFunction.fshttps://github.com/WIP: Nameof operator #13/files#diff-25 (8)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/E_NameOfStringConst.fshttps://github.com/WIP: Nameof operator #13/files#diff-26 (7)
  • A tests/fsharpqa/Source/Conformance/Expressions/DataExpressions/NameOf/env.lsthttps://github.com/WIP: Nameof operator #13/files#diff-27 (9)
  • M tests/fsharpqa/Source/test.lsthttps://github.com/WIP: Nameof operator #13/files#diff-28 (2)

Patch Links:


Reply to this email directly or view it on GitHubhttps://github.com//pull/13.

@ctaggart
Copy link
Contributor

Looking forward to this functionality being added at some point. I think it will be a great addition. I could really use it for some logging I'm doing right now.

@forki
Copy link
Contributor Author

forki commented Jan 16, 2015

Could you please describe your scenario with a small code sample?

@dsyme dsyme changed the title Nameof operator WIP: Nameof operator Jan 17, 2015
@dsyme
Copy link
Contributor

dsyme commented Jan 21, 2015

This won't be in F# 4.0, and needs further work, so I suggest we "archive" this PR for now by closing it, and then revisiting it later.

@dungpa
Copy link
Contributor

dungpa commented Jan 21, 2015

Could you add some sort of labels e.g. "archived", "deferred", etc. so that it is easier to track these issues?

@dsyme
Copy link
Contributor

dsyme commented Jan 21, 2015

Yes, we would need an "archived" label. The feature is still in http://fslang.uservoice.com of course, which sort of acts as a root for all future feature work on the core language/library

@forki
Copy link
Contributor Author

forki commented Jan 21, 2015

(just a small technical note: the branch should be kept in at least one repo, otherwise it might get lost after closing)

@latkin
Copy link
Contributor

latkin commented Jan 21, 2015

"archived" it is

@latkin latkin closed this Jan 21, 2015
@xperiandri
Copy link
Contributor

What is the status of this feature?
Does someone do any work on this?

@forki
Copy link
Contributor Author

forki commented Jul 20, 2015

I created a prototype, but there are many open edge cases.
On Jul 20, 2015 1:55 AM, "Andrii Chebukin" [email protected] wrote:

What is the status of this feature?
Does someone do any work on this?


Reply to this email directly or view it on GitHub
#13 (comment)
.

@ghost
Copy link

ghost commented Nov 24, 2015

Look forward to this feature. I assume it will work also doing interop with C# code.

@ghost
Copy link

ghost commented Dec 1, 2015

Example usecase:
[<ReflectedDefinition(true)>]
let foo (x) = ...

let fooType = foo.GetType().DeclaringType.GetMethod(nameof(foo))

@forki
Copy link
Contributor Author

forki commented Dec 1, 2015

@bluemmc yes there are many usecases, but team decided against it for now.

@ghost
Copy link

ghost commented Dec 1, 2015

Yes, could read that it would not make it for F# 4.0 - but that is out now. What about next F# versions - has that been decided ?

@forki
Copy link
Contributor Author

forki commented Dec 1, 2015

@KevinRansom @otawfik-ms @NumberByColors ?

@7sharp9
Copy link
Contributor

7sharp9 commented Jan 29, 2016

Would of been nice to have the open edge cases listed so that anyone interested could look at them etc.

@forki
Copy link
Contributor Author

forki commented Jan 29, 2016

Unfortunately I can't reopen this after @latkin closed it.

IIRC we had some discussion over there. It was mostly that we don't support all the positions where nameof can be used in C# (e.g. in attributes)

@forki
Copy link
Contributor Author

forki commented Jan 29, 2016

From the discussion here you can tell that the interest inside ms is rather low ;-)

@ghost
Copy link

ghost commented Jan 29, 2016

Yes, unfortunately no response from the F# team.

Nameof is the one of few place where C# has a strong advantage language-wise. It is highly annoying that F# still forces developers to use a string anti-pattern when referencing stuff.

@dsyme Is it possible to clarify if there are any plans for Nameof support in F# ?

@enricosada
Copy link
Contributor

well, i think this pr it's dead (need rebase anyway).
But i think it's possibile to reboot a [WIP] pr if someone want to try implement it.

It's a really nice feature to have, and a good base implementation by @forki

@forki
Copy link
Contributor Author

forki commented Jan 29, 2016

Ok I will try to rebase. Then we can let it rot again.

@ghost
Copy link

ghost commented Jan 29, 2016

RE: IIRC we had some discussion over there. It was mostly that we don't support all the positions where nameof can be used in C# (e.g. in attributes)

@forki Would be better to release something a partial fix, rather then letting it rot as you write. Hopefully the F# team can see the benefits of your contribution after a rebase and another look?

@enricosada
Copy link
Contributor

i really hope not @forki, it's a really nice feature.
And now VS 2015 shipped, so less problems

@forki
Copy link
Contributor Author

forki commented Jan 29, 2016

rebased version at #908

nelak pushed a commit to nelak/visualfsharp that referenced this pull request Nov 27, 2016
kevmal pushed a commit to kevmal/visualfsharp that referenced this pull request Mar 22, 2019
append extension methods in GetRelevantMethodsForTrait
nojaf pushed a commit to nojaf/fsharp that referenced this pull request Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants