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

Avoiding long cast expresion after filtering

2 replies
Linas
Joined: 2009-09-02,
User offline. Last seen 42 years 45 weeks ago.

Hello,

I find myself frequently storing items that derive from the same base
type in lists and then filtering those lists to select items matching
some specific subtype. Like this:

val genericList = List[GenericType](some items);
val specificList = genericList.filter(_.isInstanceOf[SpecificType]);

After filtering is done, if I want to use the stuff that is specific to
all items in "specificList" I either have to manually cast the
"specificList" to "List[SomeType]" or cast every item to "SomeType". The
cast expression is long:

val specificList =
genericList.filter(_.isInstanceOf[SpecificType]).asInstanceOf[List[SpecificType]];

If we filter for example HashMaps, the cast gets even longer, and if we
use both mutable and unmutable collections in the same code and prefix
collection types with package names, it gets even longer again.

Would it be reasonable to request the addition of some "cast" method to
collection types that would recast collection to a different type
without the need to type the long asInstanceOf[...]? Like this:

val specificList =
genericList.filter(_.isInstanceOf[SpecificType]).cast[SpecificType];

Which would result in "specificList" being of type List[SpecificType];

Linas.

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Avoiding long cast expresion after filtering
genericList.flatMap {
  case v: SpecificType => Some(v)
  case _ => None
}

On Tue, Mar 16, 2010 at 2:36 PM, Linas <vejobrolis [at] gmail [dot] com> wrote:
Hello,

I find myself frequently storing items that derive from the same base
type in lists and then filtering those lists to select items matching
some specific subtype. Like this:

val genericList = List[GenericType](some items);
val specificList = genericList.filter(_.isInstanceOf[SpecificType]);

After filtering is done, if I want to use the stuff that is specific to
all items in "specificList" I either have to manually cast the
"specificList" to "List[SomeType]" or cast every item to "SomeType". The
cast expression is long:

val specificList =
genericList.filter(_.isInstanceOf[SpecificType]).asInstanceOf[List[SpecificType]];

If we filter for example HashMaps, the cast gets even longer, and if we
use both mutable and unmutable collections in the same code and prefix
collection types with package names, it gets even longer again.

Would it be reasonable to request the addition of some "cast" method to
collection types that would recast collection to a different type
without the need to type the long asInstanceOf[...]? Like this:

val specificList =
genericList.filter(_.isInstanceOf[SpecificType]).cast[SpecificType];

Which would result in "specificList" being of type List[SpecificType];

Linas.








--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Avoiding long cast expresion after filtering
Or, in Scala 2.8:
genericList partialMap { case x: SpecificType => x }
This has the advantage that the result has the type you expect, and also that no intermediary objects are required for the process.

On Tue, Mar 16, 2010 at 10:46 PM, David Pollak <feeder [dot] of [dot] the [dot] bears [at] gmail [dot] com> wrote:
genericList.flatMap {
  case v: SpecificType => Some(v)
  case _ => None
}

On Tue, Mar 16, 2010 at 2:36 PM, Linas <vejobrolis [at] gmail [dot] com> wrote:
Hello,

I find myself frequently storing items that derive from the same base
type in lists and then filtering those lists to select items matching
some specific subtype. Like this:

val genericList = List[GenericType](some items);
val specificList = genericList.filter(_.isInstanceOf[SpecificType]);

After filtering is done, if I want to use the stuff that is specific to
all items in "specificList" I either have to manually cast the
"specificList" to "List[SomeType]" or cast every item to "SomeType". The
cast expression is long:

val specificList =
genericList.filter(_.isInstanceOf[SpecificType]).asInstanceOf[List[SpecificType]];

If we filter for example HashMaps, the cast gets even longer, and if we
use both mutable and unmutable collections in the same code and prefix
collection types with package names, it gets even longer again.

Would it be reasonable to request the addition of some "cast" method to
collection types that would recast collection to a different type
without the need to type the long asInstanceOf[...]? Like this:

val specificList =
genericList.filter(_.isInstanceOf[SpecificType]).cast[SpecificType];

Which would result in "specificList" being of type List[SpecificType];

Linas.








--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

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