Seyed H. HAERI (Hossein | 19 Aug 2012 14:29
Picon
Gravatar

Implicit Conversion Not Applied Automatically

Dear all,

I have a class C for an instance c of which I would like the expression

c < b

to have a certain meaning. However, in my application, I would rather do so without touching C itself. To that end, I implemented a very simple wrapper around C which merely augments C with a < operator:

case class CLessThanWaiter(c: C) {
  def < (b: B) = ...
}

I also have an implicit conversion c2CLTW in my package object for getting C instances silently converted into CLessThanWaiter. Strangely enough, for a code fragment c < b, the compiler complains about C not having a member <, whilst things run perfectly when I change that into c2CLTW(c) < b. How come is that really possible? It looks to me like the compiler somehow fails to automatically understand that the implicit is applicable. Yet, it doesn't complain about its applicability when I use it manually. Is that a bug?

I'm happy to provide my exact pieces of code if you think that helps in the diagnosis. Just didn't want to clutter the question with unrelated material...

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Jason Zaugg | 19 Aug 2012 15:06
Picon
Gravatar

Re: Implicit Conversion Not Applied Automatically

On Sun, Aug 19, 2012 at 2:29 PM, Seyed H. HAERI (Hossein)
<hossein.haeri@...> wrote:
> Dear all,
>
> I have a class C for an instance c of which I would like the expression
>
> c < b
>
> to have a certain meaning. However, in my application, I would rather do so
> without touching C itself. To that end, I implemented a very simple wrapper
> around C which merely augments C with a < operator:
>
> case class CLessThanWaiter(c: C) {
>   def < (b: B) = ...
> }
>
> I also have an implicit conversion c2CLTW in my package object for getting C
> instances silently converted into CLessThanWaiter. Strangely enough, for a
> code fragment c < b, the compiler complains about C not having a member <,
> whilst things run perfectly when I change that into c2CLTW(c) < b. How come
> is that really possible? It looks to me like the compiler somehow fails to
> automatically understand that the implicit is applicable. Yet, it doesn't
> complain about its applicability when I use it manually. Is that a bug?
>
> I'm happy to provide my exact pieces of code if you think that helps in the
> diagnosis. Just didn't want to clutter the question with unrelated
> material...

You might like to troubleshoot it further with scalac -Yinfer-debug.

Otherwise, post a small code sample to demonstrate.

-jason


Gmane