Search code examples
f#nameof

F# nameof for record property


I'm trying to use the new nameof feature in F# 5.0 preview. It works for values but not for record properties, e.g.:

type MyType { Id: int }
let name = nameof MyType.Id

This results in the error FS0728 Field 'Id' is not static

I tried doing:

let name = nameof<MyType.Id>
let name = nameof(MyType.Id)
let name = nameof Id

And neither fix the error. Is there a special way I'm supposed to do this or was nameof not fully implemented?


Solution

  • This is a duplicate of F# nameof operator not a first-class function.

    The short answer is:

    let x = Unchecked.defaultof<MyType>
    let name = nameof x.Id