Created
December 12, 2009 06:57
-
-
Save retronym/254774 to your computer and use it in GitHub Desktop.
Proposal for exposing parameter names to case class toString or to any method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait NamedElementsProduct extends Product { | |
def namedElements: Iterator[(String, Any)] | |
} | |
trait HaskellLikeToString extends NamedElementsProduct { | |
override def toString: String = namedElements.map((p) => p._1 + "=" + p._2).mkString(productPrefix + "(", ",", ")") | |
} | |
case class A(a: Int, b: Int) extends NamedElementsProduct with HaskellLikeToString { | |
def namedElements = List(("a", a), ("b", b)).iterator | |
} | |
// with compiler, or compiler plugin, support: | |
//case class A(a: Int, b: Int) extends HaskellLikeToString | |
assert(A(1, 2).toString == "A(a=1,b=2)") | |
trait FunctionManifest { | |
trait ParamList { | |
val isImplicit: Boolean | |
val params: List[Param] | |
} | |
trait Param[T] { | |
val name: String | |
val default: Option[T] | |
val synthetic: Boolean | |
} | |
def name: String | |
def paramLists: List[ParamList] | |
} | |
// More generally, you could request an implicit manifest of the currenct function: | |
def a(a: Int)(implicit pm: FunctionManifest) = { | |
"you called " + pm.name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment