- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Implicit in constructor
Wed, 2012-02-01, 15:06
Hello All,
I've been doing some experiences with dependency injection using
some of the scala features and one thing got my atencion about
Implicits in the constructor. Althought i can use:
class Computer(implicit val keyboard: InputDevice)
object Test{
implicit val keyboard = new InputDevice
val pc = new Computer
}
I can not do the following:
object Test{
val pc = new Computer (new InputDevice)
}
Looking for some info at the web, i found a old discussion (http://
scala-programming-language.1934581.n4.nabble.com/Strange-limitation-
implicit-in-constructor-td1996011.html) where is said that this
happens only because of historical motives and that could be
redesigned.
I wanted to know if i could expect that to be "fixed" on a next
scala version, so the rules for implicit in constructors and methods
be the same.
Thu, 2012-02-02, 09:51
#2
Re: Implicit in constructor
On Thu, Feb 2, 2012 at 8:48 AM, rkuhn <google@rkuhn.info> wrote:
Yes, all constructors have at least one non-implicit parameter section.
class A // actually class A()
class B(implicit a: Any) // actually class B()(implicit b: Any)
new B()("b")
-jason
I seem to remember that it can be made to work by sprinkling some empty parens onto either the definition or the use site or both (sorry, can't check right now).
Regards,
Roland
Yes, all constructors have at least one non-implicit parameter section.
class A // actually class A()
class B(implicit a: Any) // actually class B()(implicit b: Any)
new B()("b")
-jason
Thu, 2012-02-02, 13:21
#3
Re: Implicit in constructor
Yeah, I could use it the following way:
object Test{
val pc = new Computer ()(new InputDevice)
}
But what is really bugging me is why do i have one behavior for
implicit in methods and another in the constructor. Searching in the
web i found an old discussion where Martin Odersky says about that:
"Maybe the rules for classes and the rules for methods should be the
same. The fact that they are not is mostly historical, because the
rules for methods had changed at a time where the rules for classes
were already fixed. It's a minor point though, which could be
redesigned (just need to find the time for doing that)"
What i would like to know is if that is going to be redesigned on the
next releases or it is going to stay that way (I'm only asking that
cause, the discussion that i found is from 2008)
On Feb 2, 5:48 am, Jason Zaugg wrote:
> On Thu, Feb 2, 2012 at 8:48 AM, rkuhn wrote:
> > I seem to remember that it can be made to work by sprinkling some empty
> > parens onto either the definition or the use site or both (sorry, can't
> > check right now).
>
> > Regards,
>
> > Roland
>
> Yes, all constructors have at least one non-implicit parameter section.
>
> class A
> // actually class A()
>
> class B(implicit a: Any)
> // actually class B()(implicit b: Any)
>
> new B()("b")
>
> -jason
I seem to remember that it can be made to work by sprinkling some empty parens onto either the definition or the use site or both (sorry, can't check right now).
Regards,
Roland