This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why is flatten declared on GenericTraversableTemplate and not TraversableLike

2 replies
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
I asked this on StackOverflow but it had no response. (http://stackoverflow.com/questions/1802828/why-is-flatten-declared-on-ge...).



The signature of TraversableLike.flatMap is as follows:
def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th
The signature of GenericTraversableTemplate.flatten is:
def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B]
Why is the latter method (which seems to me to differ in usage from flatMap only in the sense that the transformer function is implicit) not definable on TraversableLike as:
def flatten[B, Th](implicit asTraversable: (A) => Traversable[B], 
                   implicit bf : CanBuildFrom[Repr, B, Th]) : Th
Is there some reason that this must be the case?


Chris


Use Hotmail to send and receive mail from your different email accounts. Find out how.
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Why is flatten declared on GenericTraversableTemplate and n
BTW - if I have posted this on the wrong list (possibly it belongs on scala user) then let me know

From: oxbow_lakes@hotmail.com
To: scala-internals@listes.epfl.ch
Subject: [scala-internals] Why is flatten declared on GenericTraversableTemplate and not TraversableLike
Date: Fri, 27 Nov 2009 14:57:27 +0000

.ExternalClass .ecxhmmessage P {padding:0px;} .ExternalClass body.ecxhmmessage {font-size:10pt;font-family:Verdana;} I asked this on StackOverflow but it had no response. (http://stackoverflow.com/questions/1802828/why-is-flatten-declared-on-ge...).



The signature of TraversableLike.flatMap is as follows:
def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th
The signature of GenericTraversableTemplate.flatten is:
def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B]
Why is the latter method (which seems to me to differ in usage from flatMap only in the sense that the transformer function is implicit) not definable on TraversableLike as:
def flatten[B, Th](implicit asTraversable: (A) => Traversable[B], 
                   implicit bf : CanBuildFrom[Repr, B, Th]) : Th
Is there some reason that this must be the case?


Chris


Use Hotmail to send and receive mail from your different email accounts. Find out how.
New! Receive and respond to mail from other email accounts from within Hotmail Find out how.
Adriaan Moors
Joined: 2009-04-03,
User offline. Last seen 42 years 45 weeks ago.
Re: Why is flatten declared on GenericTraversableTemplate and
Indeed, by analogy to flatMap, this might be a better signature (&implementation) for flatten in TraversableLike:
  def flatten[B](implicit asTraversable: A => Traversable[B], bf: CanBuildFrom[Repr, B, That]): That = flatMap(asTraversable)
If no-one objects, I'll remove the old flatten implementation and move this one-liner to TraversableLike

cheers
adriaan

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland