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

Abstract types / Generics / Collect

6 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all,I have the following trait with an abstract type
trait RepriceableFrom {  type T <: TermStructure
  def repriceFrom(cashflowsTermStructure: T, discountTermStructure: T) }
object RepriceableFrom {  type GenericRepriceable[X] = RepriceableFrom {type T = X}

}
and I try to define the following class
class NoArbitrageObjectiveFunction(securitiesCollections:RepriceableFrom*) {
  protected val fixedStepTermStructureRepriceable=securitiesCollections.collect{case collection:GenericRepriceable[FixedStepTermStructure]=>collection}
  protected val variableStepTermStructureRepriceable=securitiesCollections.collect{case collection:GenericRepriceable[VariableStepTermStructure]=>collection}  protected val interpolatedTermStructureRepriceable=securitiesCollections.collect{case collection :GenericRepriceable[InterpolatedTermStructure]=>collection}


}
However,  the following test fails:
class NoArbitrageObjectiveFunctionTest extends FunSuite {
  test("A no arbitrage objective function should split the collections according to which term structure they need to be repriced "){
    val function = new NoArbitrageObjectiveFunction(variableStepTermStructureCollection,variableStepTermStructureCollection,fixedStepTermStructureCollection,interpolatedStepTermStructureCollection){       def fixedCount = fixedStepTermStructureRepriceable.size;      def interpolatedCount = interpolatedTermStructureRepriceable.size;      def variableCount = variableStepTermStructureRepriceable.size;     }
    assert(function.fixedCount===1)    assert(function.interpolatedCount===1)    assert(function.variableCount===1)
  }
  private def variableStepTermStructureCollection:RepriceableFrom = new RepriceableFrom{    type T=VariableStepTermStructure;    def repriceFrom(cashflowsTermStructure: VariableStepTermStructure, discountTermStructure: VariableStepTermStructure) = println("variable");
  }
  private def fixedStepTermStructureCollection:RepriceableFrom = new RepriceableFrom{    type T=FixedStepTermStructure;    def repriceFrom(cashflowsTermStructure: FixedStepTermStructure, discountTermStructure: FixedStepTermStructure) = println("fixed");
  }  private def interpolatedStepTermStructureCollection:RepriceableFrom = new RepriceableFrom{    type T=InterpolatedTermStructure;    def repriceFrom(cashflowsTermStructure: InterpolatedTermStructure, discountTermStructure: InterpolatedTermStructure) = println("interpolated");
  }
}
Why does the collect not behave as expected?
Best RegardsEdmondo
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Abstract types / Generics / Collect
I think this has to do withhttps://issues.scala-lang.org/browse/SI-5143 Any suggestions?
Best Regards

2012/1/9 Edmondo Porcu <edmondo.porcu@gmail.com>
Dear all,I have the following trait with an abstract type
trait RepriceableFrom {  type T <: TermStructure
  def repriceFrom(cashflowsTermStructure: T, discountTermStructure: T) }
object RepriceableFrom {  type GenericRepriceable[X] = RepriceableFrom {type T = X}

}
and I try to define the following class
class NoArbitrageObjectiveFunction(securitiesCollections:RepriceableFrom*) {
  protected val fixedStepTermStructureRepriceable=securitiesCollections.collect{case collection:GenericRepriceable[FixedStepTermStructure]=>collection}
  protected val variableStepTermStructureRepriceable=securitiesCollections.collect{case collection:GenericRepriceable[VariableStepTermStructure]=>collection}  protected val interpolatedTermStructureRepriceable=securitiesCollections.collect{case collection :GenericRepriceable[InterpolatedTermStructure]=>collection}


}
However,  the following test fails:
class NoArbitrageObjectiveFunctionTest extends FunSuite {
  test("A no arbitrage objective function should split the collections according to which term structure they need to be repriced "){
    val function = new NoArbitrageObjectiveFunction(variableStepTermStructureCollection,variableStepTermStructureCollection,fixedStepTermStructureCollection,interpolatedStepTermStructureCollection){       def fixedCount = fixedStepTermStructureRepriceable.size;      def interpolatedCount = interpolatedTermStructureRepriceable.size;      def variableCount = variableStepTermStructureRepriceable.size;     }
    assert(function.fixedCount===1)    assert(function.interpolatedCount===1)    assert(function.variableCount===1)
  }
  private def variableStepTermStructureCollection:RepriceableFrom = new RepriceableFrom{    type T=VariableStepTermStructure;    def repriceFrom(cashflowsTermStructure: VariableStepTermStructure, discountTermStructure: VariableStepTermStructure) = println("variable");
  }
  private def fixedStepTermStructureCollection:RepriceableFrom = new RepriceableFrom{    type T=FixedStepTermStructure;    def repriceFrom(cashflowsTermStructure: FixedStepTermStructure, discountTermStructure: FixedStepTermStructure) = println("fixed");
  }  private def interpolatedStepTermStructureCollection:RepriceableFrom = new RepriceableFrom{    type T=InterpolatedTermStructure;    def repriceFrom(cashflowsTermStructure: InterpolatedTermStructure, discountTermStructure: InterpolatedTermStructure) = println("interpolated");
  }
}
Why does the collect not behave as expected?
Best RegardsEdmondo

GerardMurphy
Joined: 2012-01-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Abstract types / Generics / Collect

I can't run this code on my current computer, but making a guess, did
the tests fail because the sizes all turned out to be 3?

If so, consider that GenericRepriceable[X] is when all is said and
done a generic type - with erasure of its X type parameter.

If I read your code correctly (and that assumption is worth
challenging), you are trying to pattern match against the erasure of
the runtime type GenericRepriceable[X] for specific X's - this isn't
supported, right?

Regards,

Gerard

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: Abstract types / Generics / Collect

my money is on type erasure

-------- Original-Nachricht --------
> Datum: Mon, 9 Jan 2012 04:39:56 -0800 (PST)
> Von: Gerard Murphy
> An: scala-user
> Betreff: [scala-user] Re: Abstract types / Generics / Collect

> Caveat: you are getting advice from a novice.
>
> I can't run this code on my current computer, but making a guess, did
> the tests fail because the sizes all turned out to be 3?
>
> If so, consider that GenericRepriceable[X] is when all is said and
> done a generic type - with erasure of its X type parameter.
>
> If I read your code correctly (and that assumption is worth
> challenging), you are trying to pattern match against the erasure of
> the runtime type GenericRepriceable[X] for specific X's - this isn't
> supported, right?
>
> Regards,
>
> Gerard

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: Abstract types / Generics / Collect
Erasure.  This is what I use:
  /**   * Filters the provided collection for elements that are assignable from the class of the type parameter   */  def filterByType[X : Manifest](in: Traversable[_]): Seq[X] = {     in.collect {      case x if manifest[X].erasure.isInstance(x) ⇒ x.asInstanceOf[X]    }.toSeq  }
-0xe1a
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Re: Abstract types / Generics / Collect
The few chars saved by
    val ys = filterByType[Y](xs)
Over
    val ys = xs collect { case y: Y => y }
Do not justify the loss of collection shape; in my opinion. Of course, it may be the case that you are in a method where Y is a type parameter rather than a type but I have yet to come across this scenario.

Chris

Date: Tue, 10 Jan 2012 10:48:20 -0800
Subject: Re: [scala-user] Re: Abstract types / Generics / Collect
From: alex@cluonflux.com
To: scala-user@googlegroups.com

Erasure.  This is what I use:
  /**   * Filters the provided collection for elements that are assignable from the class of the type parameter   */  def filterByType[X : Manifest](in: Traversable[_]): Seq[X] = {     in.collect {      case x if manifest[X].erasure.isInstance(x) ⇒ x.asInstanceOf[X]    }.toSeq  }
-0xe1a
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Re: Abstract types / Generics / Collect


2012/1/11 Chris Marshall <oxbow_lakes@hotmail.com>
The few chars saved by
    val ys = filterByType[Y](xs)
Over
    val ys = xs collect { case y: Y => y }
Do not justify the loss of collection shape; in my opinion. Of course, it may be the case that you are in a method where Y is a type parameter rather than a type but I have yet to come across this scenario.

+1
 

Chris

Date: Tue, 10 Jan 2012 10:48:20 -0800
Subject: Re: [scala-user] Re: Abstract types / Generics / Collect
From: alex@cluonflux.com
To: scala-user@googlegroups.com

Erasure.  This is what I use:
  /**   * Filters the provided collection for elements that are assignable from the class of the type parameter   */   def filterByType[X : Manifest](in: Traversable[_]): Seq[X] = {     in.collect {      case x if manifest[X].erasure.isInstance(x) ⇒ x.asInstanceOf[X]    }.toSeq  }
-0xe1a



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang

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