- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Strange implicit problem
Fri, 2012-02-10, 10:24
Dear all,I have a problem with implicit resolution.
I have two classes, A and B. and when from A I call b.method(b.field) the implicit is resolved correctly.
If on the contrary I call
b.method2 = {method(field)}
The implicit is not resolved correctly. I have no idea of why since the imports looks the same. I have attached the source with a small scalatest you can run, it would be great if you could help me with that.
Best Regards
Edmondo
I have two classes, A and B. and when from A I call b.method(b.field) the implicit is resolved correctly.
If on the contrary I call
b.method2 = {method(field)}
The implicit is not resolved correctly. I have no idea of why since the imports looks the same. I have attached the source with a small scalatest you can run, it would be great if you could help me with that.
Best Regards
Edmondo
Sun, 2012-02-12, 09:31
#2
Re: Strange implicit problem
since i ran into a little problem involving bitlocker, i had some time
to spend for your problem.
the problem spot:
def repriceFrom[T<:TermStructure](termStructure:
MultiCurveTermStructure[T],discountType:DiscountType) = {
reprice(termStructure,fixedStepTermStructureItems,discountType)(compiler, plz
insert my implicit here);
}
def reprice[Z<:TermStructure,K<:TermStructure](termStructure:
MultiCurveTermStructure[K],
items:IndexedSeq[MultiCurveRepriceableCollection[Z]],
discountType:DiscountType)(implicit
converter:MultiCurveTermStructureConverter[K, Z] ){
the implicit def that you want to use here is:
implicit def
SameConverter[X<:TermStructure]:MultiCurveTermStructureConverter[X, X] = new
MultiCurveTermStructureConverter[X,X](Some(identity))
now the problem:
you want to put a MultiCurveTermStructure[X,X] in there. that is only
possible if Z and K are equal.
the type of Z is known to the compiler, it can get if from
fixedStepTermStructureItems. but the compiler does not know the exact
type of T. that information is only known at the spot where you pass the
actual collection. inside the method, it can only say it is <:
TermStructure. so T != Z and therefore Z != K and therefore, the
implicit does not fit in there.
a simpler version of the problem:
def broken[T <: Any](t:T) {
method(t)(implicit val that works only on strings)
}
this will work:
method("hello world") // compiler knows it's a string because it's
totally obvious even to a monkey :)
but inside the method broken, this cannot work - how could it, since you
can call it like this:
broken("asdf")
broken(5)
broken(List(1,2,3,4))
the implicit is picked at compile time. do you see the impossibility to
pick the correct one?
Am 10.02.2012 11:24, schrieb Dennis Haupt:
> i didn't have to time to take a deeper look at it, but the only conversion i could make explicit in the testcase is:
>
> reprice(termStructure,fixedStepTermStructureItems,discountType)(MultiCurveTermStructure.noMultiCurveTermStructureConversionAvailable);
>
> for all others, i get different compile errors
>
>
>
>
> -------- Original-Nachricht --------
>> Datum: Fri, 10 Feb 2012 10:24:39 +0100
>> Von: Edmondo Porcu
>> An: scala-user
>> Betreff: [scala-user] Strange implicit problem
>> Dear all,
>> I have a problem with implicit resolution.
>>
>> I have two classes, A and B. and when from A I call b.method(b.field) the
>> implicit is resolved correctly.
>>
>> If on the contrary I call
>>
>> b.method2 = {
>> method(field)
>> }
>>
>> The implicit is not resolved correctly. I have no idea of why since the
>> imports looks the same. I have attached the source with a small scalatest
>> you can run, it would be great if you could help me with that.
>>
>> Best Regards
>>
>> Edmondo
i didn't have to time to take a deeper look at it, but the only conversion i could make explicit in the testcase is:
reprice(termStructure,fixedStepTermStructureItems,discountType)(MultiCurveTermStructure.noMultiCurveTermStructureConversionAvailable);
for all others, i get different compile errors
-------- Original-Nachricht --------
> Datum: Fri, 10 Feb 2012 10:24:39 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Strange implicit problem
> Dear all,
> I have a problem with implicit resolution.
>
> I have two classes, A and B. and when from A I call b.method(b.field) the
> implicit is resolved correctly.
>
> If on the contrary I call
>
> b.method2 = {
> method(field)
> }
>
> The implicit is not resolved correctly. I have no idea of why since the
> imports looks the same. I have attached the source with a small scalatest
> you can run, it would be great if you could help me with that.
>
> Best Regards
>
> Edmondo