I'm trying to implement a HashMap-based tree that'd support O(1) subtree lookup for a given root key. To that goal, I'm trying to do the following:
scala> type Q = HashMap[Char, Q]
<console>:6: error: illegal cyclic reference involving type Q
type Q = HashMap[Char, Q]
^
So the question is, is there a way for me to do something of the sort without resorting to the ugly HashMap[Char, Any] with subsequent casting of values to HashMap[Char, Any]?
Now, I also see that I can use something like the following to avoid the cyclic-reference error, and it might even be cleaner -- but it'd be nice to find out how to correctly do it the first way, just for the educational value.
import collections.mutable.HashMap
class LTree {
val children = new HashMap[Char, LTree]
}
Thanks a bunch.
Charand whose nodes bear no information at all at all? If so, yourLTreeis about as minimal as it gets, though as written you can only create empty trees sincechildrenis immutable as is theHashMapitself and the constructor takes no arguments. - Randall SchulzLTreeis more appropriate as I can toss in fields and member functions inside it for various tree ops. - Paul Milovanovtypedeclarations only name aliases for already existing types, they don't invent new ones. In this, Scala'stypeis like Haskell'stype, not itsnewtype. - Randall SchulzIn Haskell's 18 years on the programming language scene, it's been in the perpetual role of the underused theoretical ideal alternative. I can't count how many times I've seen "perhaps Haskell?" since researching languages became a hobby of mine. I'm seeing more and more programmers getting real work with Scala, not musing about its theoretical possibilities. I find that pragmatism encouraging.- Paul Milovanov