Scala: working around the "illegal cyclic reference"

Posted by Paul Milovanov on Stack Overflow See other posts from Stack Overflow or by Paul Milovanov
Published on 2010-04-28T19:15:56Z Indexed on 2010/04/28 22:17 UTC
Read the original article Hit count: 400

Filed under:
|
|

Hi all,

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.

© Stack Overflow or respective owner

Related posts about scala

Related posts about types