Skip to content

Instantly share code, notes, and snippets.

@retronym
Created December 12, 2009 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronym/254774 to your computer and use it in GitHub Desktop.
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
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