Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular dependency in Scala collections

Trait Traversable has methods such as toList, toMap, ToSeq. Given that List, Map, Seq are subclasses of Traversable, this creates a circular dependency, which is generally not a desirable design pattern.

I understand that this is constrained to the collections library and it provides some nice transformation methods.

Was there any alternative design considered? Such as a "utility" class, or adding the conversion methods to Predef?

Say I want to add a new class: class RandomList extends List {...}. It would be nice to have a method toRandomList available for all Traversable classes, but for that I would need to "pimp my library" with an implicit on Traversable? This seems a bit of an overkill. With a utility class design, I could just extend that class (or Predef) to add my conversion method. What would be the recommended design here?

like image 518
Adrian Avatar asked Nov 30 '22 17:11

Adrian


1 Answers

An alternative and extensible approach would be to[List], to[RandomList].

It's a bit tricky to add this with implicits, though. https://gist.github.com/445874/2a4b0bb0bde29485fec1ad1a5bbf968df80f2905

like image 185
retronym Avatar answered Dec 05 '22 12:12

retronym