dwickern / scala-nameof

Get the name of an variable, function, class member, or type as a string--at compile-time!
MIT License
131 stars 23 forks source link

Can I use nameOf[T](function) to get the field name #18

Open CowSmiles opened 3 years ago

CowSmiles commented 3 years ago

Thanks for your awesome work.

I have a problem using nameof without having an instance of the type.

We can use the following way to get a field name

case class Person(name: String, age: Int)

println(nameOf((_:Person).age)) // age
println(nameOf[Person](_.age)) // age

But if I make (_:Person).age to a parameter, so we only get the parameter name, how can i get the real field name according the function:

val foo = (_:Person).age
println(nameOf(foo)) // foo, but I want age
println(nameOf[Person](foo)) // foo, but I want age

Thanks

plmuninn commented 2 years ago

I can say I have same issue - I'm trying to pass expr: T => Any into nameOf[T] and what I'm getting is name of parameter passed to it.