- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Implicit Objects Not Found for Singleton Types
Sat, 2011-06-04, 17:45
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.
Sat, 2011-06-25, 12:07
#2
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
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