6

Scala's collection library contains the forwarders IterableForwarder, TraversableForwarder, SeqForwarder and proxies like IterableProxy, MapProxy, SeqProxy, SetProxy, TraversableProxy, etc. Forwarders and proxies both delegate collection methods to an underlying collection object. The main difference between these two are that forwarders don't forward calls that would create new collection objects of the same kind.

In which cases would I prefer one of these types over the other? Why and when are forwarders useful? And if they are useful why are there no MapForwarder and SetForwarder?

I assume proxies are most often used if one wants to build a wrapper for a collection with additional methods or to pimp the standard collections.

Frank S. Thomas
  • 4,725
  • 2
  • 28
  • 47

1 Answers1

4

I think this answer provides some context about Proxy in general (and your assumption about wrapper and pimping would be correct).

As far as I can tell the subtypes of Proxy are more targeted to end users. When using Proxy the proxy object and the self object will be equal for all intent and purposes. I think that's actually the main difference. Don't use Proxy if that assumption does not hold.

The Forwarder traits only seems to be used to support ListBuffer and may be more appropriate if one needs to roll out their own collection class built on top of the CanBuildFrom infrastructure. So I would say it's more targeted to library writers where the library is based on the 2.8 collection design.

Community
  • 1
  • 1
huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • Thanks for your answer! That the _Forwarder_ traits are only used for `ListBuffer` may answer the question why there are no `MapForwarder` and `SetForwarder`: because they are not required for implementing `ListBuffer`. – Frank S. Thomas Apr 21 '11 at 09:33