I have the following lines of code I want to port from C# to F#:
private const string TypeName = nameof(MyClass);
private const string MemberName = nameof(MyClass.MyMember);
The value of TypeName
then is "MyClass"
and the value of MemberName
then is "MyMember"
. What do I have to write in F#?
As mentioned in the comments, a proper
nameof
operator is work in progress.In the meantime, you can use F# quotations (similar to C# expression trees) to implement similar functionality - it is a bit uglier and requires some discipline (you need to put actual member access in the quotation), but it at least checks that the member exists and prevents typos:
The
any
definition is just a generic value that you can use to refer to instance members: