Saturday, September 13, 2008

Language Feature Request

Maybe this feature exists in some language, I'm not sure. My inexperience is letting me down. :( Here is what I want, as demonstrated in Scala.

I want to be able to use the name of my variable programmatically. So, instead of having to give my objects names like this:


case class Server( name: String )
val server = Server("Altair")
println(server)


Which yields: Server("Altair")


or like this:


class Server( name: String ){ override def toString = name }
val server = new Server("Altair")
println(server)


Which yields simply: Altair


I would like something like this:


case class Server extends VariableName
val Altair = new Server
println(Altair)


Which yields simply: Altair

That example shows it as simply a library. It probably can't be done as a library, so it seems like it would have to be a language feature. Something like this.


varname case class Server
val Altair = new Server
println(Altair)


Which yields simply: Altair


Does anything like this exist? Would it be terribly difficult to build into a compiler? Things would probably be tricky if you said something like...


varname case class Server
val Altair = new Server
val Moxy = Altair
println(Moxy)


Would you want to get Altair, or Moxy?

Can anyone give any opinions on this at all?