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

Implicit Objects Not Found for Singleton Types

2 replies
Chris Sachs
Joined: 2011-06-04,
User offline. Last seen 42 years 45 weeks ago.

Hello,
is there any reason that an implicit object shouldn't be supplied as
an implicit parameter whose type is the Singleton type of the object
itself?

scala> implicit object foo
defined module foo

scala> implicitly[foo.type]
:9: error: could not find implicit value for parameter e:
foo.type
implicitly[foo.type]
^

Reassigning the object to an implicit val yields success.

scala> implicit val bar = foo
bar: foo.type = foo$@3f375021

scala> implicitly[bar.type]
res1: bar.type = foo$@3f375021

Is this a bug or is there a distinction between objects and vals that
I'm missing?

Thanks.

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Implicit Objects Not Found for Singleton Types

On Sat, Jun 4, 2011 at 5:45 PM, Chris Sachs wrote:
> Hello,
> is there any reason that an implicit object shouldn't be supplied as
> an implicit parameter whose type is the Singleton type of the object
> itself?
>
> scala> implicit object foo
> defined module foo
>
> scala> implicitly[foo.type]
> :9: error: could not find implicit value for parameter e:
> foo.type
>       implicitly[foo.type]
>                 ^
>
> Reassigning the object to an implicit val yields success.
>
> scala> implicit val bar = foo
> bar: foo.type = foo$@3f375021
>
> scala> implicitly[bar.type]
> res1: bar.type = foo$@3f375021
>
> Is this a bug or is there a distinction between objects and vals that
> I'm missing?

I can't see any obvious reason why this shouldn't work. Maybe open an
enhancement ticket?

Cheers,

Miles

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Implicit Objects Not Found for Singleton Types

On Sat, Jun 4, 2011 at 5:45 PM, Chris Sachs wrote:
> is there any reason that an implicit object shouldn't be supplied as
> an implicit parameter whose type is the Singleton type of the object
> itself?
>
> scala> implicit object foo
> defined module foo
>
> scala> implicitly[foo.type]
> :9: error: could not find implicit value for parameter e:
> foo.type
>       implicitly[foo.type]
>                 ^
>
> Reassigning the object to an implicit val yields success.
>
> scala> implicit val bar = foo
> bar: foo.type = foo$@3f375021
>
> scala> implicitly[bar.type]
> res1: bar.type = foo$@3f375021
>
> Is this a bug or is there a distinction between objects and vals that
> I'm missing?

Actually, it turns out that you can get what you're after with a
little bit of rejigging,

scala> trait ImplicitObject { implicit val implicitSelf : this.type = this }
defined trait ImplicitObject

scala> object Foo extends ImplicitObject
defined module Foo

scala> implicitly[Foo.type]
res0: Foo.type = Foo$@6258e1

Cheers,

Miles

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