Skip to content

Instantly share code, notes, and snippets.

@cowtowncoder
Last active August 29, 2015 13:59
Show Gist options
  • Save cowtowncoder/10593062 to your computer and use it in GitHub Desktop.
Save cowtowncoder/10593062 to your computer and use it in GitHub Desktop.
Trying to grok how implicits bind so that 'properties' is essentially passed/shared
class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (implicit properties: LinkedHashMap[String, ModelProperty]) {
// ...
def parse = Option(cls).map(parseRecursive(_))
def parseRecursive(hostClass: Class[_]): Unit = {
// ... and way, way deeper down the call stack
properties += name -> param
}
...
}
class XxxConverter extends ModelConverter {
def read(cls: Class[_], typeMap: Map[String, String]): Option[Model] = {
Option(cls).flatMap({
cls => {
implicit val properties = new LinkedHashMap[String, ModelProperty]()
new ModelPropertyParser(cls, typeMap).parse
val newProperties = mutable.Buffer.empty[(String, ModelProperty)]
cls.getDeclaredFields.filter(field => Modifier.isPrivate(field.getModifiers)).map(_.getName).flatMap { field =>
properties.get(field).map { value =>
properties -= field
newProperties += field -> value
}
}
newProperties ++= properties
val p = (for((key, value) <- newProperties)
yield (value.position, key, value)
).toList
// ... and so forth
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment