fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.92k stars 300 forks source link

Add preliminary support for nameof operator #748

Closed FrankBro closed 7 years ago

FrankBro commented 7 years ago

Description

With the addition of FSharp.Reflection, it is very easy to create dynamic form applications. Adding a simpler version of nameof would make it even easier and less error prone.

Repro code

type InnerRecord = {
    Float: float
}

type Record = {
    String: string
    Int: int
    InnerRecord: InnerRecord
}

let record = { String = ""; Int = 0 }
"String" = nameof<record.String>
"Int" = nameof<record.Int>
"InnerRecord.Float" = nameof<record.InnerRecord> + "." + nameof<record.InnerRecord.Float>

Expected and actual results

For those tests to pass

Related information

alfonsogarciacaro commented 7 years ago

For some reason I thought this was already implemented and forgot about it, sorry! I already fixed it (the only difference being you've to use parens instead of angled brackets nameof(record.String)) and it'll be included in the next Fable 1.0 pre-release.

nameof will also work with type references "Record" = nameof(typeof<Record>), and I also added nameofLambda as it's not always possible to have an instance reference to access the properties: nameofLambda(fun (x: Record) -> x.String). Unfortunately, it's a bit more verbose so please tell me if you've another proposal.