Quantcast

Strange limitation: implicit in constructor.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Strange limitation: implicit in constructor.

toivo-2
Hi

I can use

class Impc( implicit a: int) {
}

object Impo {
  implicit val av = 6
  val impo = new Impc
}

But following is not allowed

object Impo {
  val impo = new Impc( 6)
}

    wrong number of arguments for constructor Impc: ()(implicit int)von.lab.Impc


Why not?
At the same time method/function calls allow this.


Thanks
toivo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] Strange limitation: implicit in constructor.

Fredrik Roos
On Jan 25, 2008 11:48 AM, toivo <[hidden email]> wrote:

>
> Hi
>
> I can use
>
> class Impc( implicit a: int) {
> }
>
> object Impo {
>   implicit val av = 6
>   val impo = new Impc
> }
>
> But following is not allowed
>
> object Impo {
>   val impo = new Impc( 6)
> }
>
>     wrong number of arguments for constructor Impc: ()(implicit
> int)von.lab.Impc
>
>
> Why not?
> At the same time method/function calls allow this.
>

The implicit arguments go in the last parameter list. When you do new
Impc(6) the compiler thinks the 6 belongs in the first parameter list,
which doesn't take any parameters in this case so you get an error. To
avoid this, pass an empty parameter list (the error message hints at
this with the () before the implicit arg)

new Impc()(6)

/F
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] Strange limitation: implicit in constructor.

Matt Hellige
Although, since it works for functions, I might still call it a bug:
  scala> def f(implicit v: Int) = v
  f: (implicit Int)Int

  scala> f(6)
  res0: Int = 6

  scala> implicit val v = 5
  v: Int = 5

  scala> f
  res1: Int = 5

Matt


On Jan 25, 2008 5:47 AM, Fredrik Roos <[hidden email]> wrote:

> On Jan 25, 2008 11:48 AM, toivo <[hidden email]> wrote:
> >
> > Hi
> >
> > I can use
> >
> > class Impc( implicit a: int) {
> > }
> >
> > object Impo {
> >   implicit val av = 6
> >   val impo = new Impc
> > }
> >
> > But following is not allowed
> >
> > object Impo {
> >   val impo = new Impc( 6)
> > }
> >
> >     wrong number of arguments for constructor Impc: ()(implicit
> > int)von.lab.Impc
> >
> >
> > Why not?
> > At the same time method/function calls allow this.
> >
>
> The implicit arguments go in the last parameter list. When you do new
> Impc(6) the compiler thinks the 6 belongs in the first parameter list,
> which doesn't take any parameters in this case so you get an error. To
> avoid this, pass an empty parameter list (the error message hints at
> this with the () before the implicit arg)
>
> new Impc()(6)
>
> /F
>



--
Matt Hellige / [hidden email]
http://matt.immute.net
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] Strange limitation: implicit in constructor.

Martin Odersky
In reply to this post by Fredrik Roos
Like Frederik writes, classes always take a non-implicit parameter
list. It does not matter whether you write class Foo  or   class
Foo(),  the former is always treated as an abbreviation of the latter.
In the same vein

class Foo(implicit x: T)  is the same as
class Foo()(implicit x: T)

That explains the behavior you have seen. 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).

Cheers

 -- Martin
Loading...