- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Abstract types / Generics / Collect
Mon, 2012-01-09, 09:32
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
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
Mon, 2012-01-09, 13:41
#2
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
Mon, 2012-01-09, 15:41
#3
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
Tue, 2012-01-10, 19:51
#4
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
/** * 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
Wed, 2012-01-11, 11:11
#5
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
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
Wed, 2012-01-11, 11:31
#6
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
Best Regards
2012/1/9 Edmondo Porcu <edmondo.porcu@gmail.com>