Isn't Scala code
trait ClassicNode {
def children: List[ClassicNode]
}
class BackRefdNode(val parent: Option[BackRefdNode],
node: ClassicNode) {
val children = node.children.map { c =>
new BackRefdNode(Some(this), c)
}
}
similar to Haskell code
data ClassicNode
data BackRefdNode = BRN { parent :: Maybe BackRefdNode, node :: ClassicNode }
children1 :: ClassicNode -> [ClassicNode]
children1 _ = undefined
children :: BackRefdNode -> [BackRefdNode]
children this = map (\c -> BRN (Just this) c) (children1 (node this))
?
Or with a type class in Haskell
class GetChildren a where
children :: a -> [a]
data ClassicNode
data BackRefdNode = BRN { parent :: Maybe BackRefdNode, node :: ClassicNode }
instance GetChildren ClassicNode where
children _ = undefined
instance GetChildren BackRefdNode where
children this = map (\c -> BRN (Just this) c) (children (node this))
i.e. in double translation into Scala
trait ClassicNode
class BackRefdNode(val parent: Option[BackRefdNode],
val node: ClassicNode)
trait GetChildren[A] {
def children(a: A): List[A]
}
object GetChildren {
implicit val classicNodeGetChildren: GetChildren[ClassicNode] = _ => ???
implicit val backRefdNodeGetChildren: GetChildren[BackRefdNode] = a =>
a.node.children.map { c =>
new BackRefdNode(Some(a), c)
}
}
implicit class GetChildrenOps[A](val a: A) extends AnyVal {
def children(implicit getChildren: GetChildren[A]): List[A] =
getChildren.children(a)
}
Or maybe you mean that in Java/Scala dispatch on this
is dynamic (on contrary to static dispatch with type classes). Then please see
Dynamic dispatch in Haskell
Is the dispatch of a Haskell TypeClass dynamic?
Does GHC use dynamic dispatch with existential types?