Compiler should avoid generating unnecessary anon classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Compiler should avoid generating unnecessary anon classes

Vladimir Kirichenko-2
Having definition.

 class X { def y = 1; def z = 2 }

and using it like:

  new X { y;z }

we're having anonymous class generated. But usually this type of code
is just used to avoid boilerplate like:

 val x = new X
 x.y
 x.z
 x....

The proposal is that compiler should avoid generating anonymous
classes in such cases, performing "rewriting" code in boilerplate
style if it can. The benefits are obvious. The only thing that might
be hurt is:

new X { y;z }.getClass.newInstance

which is extremely bizarre case and could be handled with annotation
or something if someone really need it. In the other hand the code
that is naturally hierarchical (GUI building for instance) will not
generate bunch of unnecessary anonymous classes - and this is very
widespread use of such "initialization syntax".


--
Best Regards,
Vladimir Kirichenko
Reply | Threaded
Open this post in threaded view
|

Re: Compiler should avoid generating unnecessary anon classes

Paul Phillips-3
On Fri, Nov 19, 2010 at 06:32:58PM +0200, Vladimir Kirichenko wrote:
> we're having anonymous class generated. But usually this type of code
> is just used to avoid boilerplate like:
>
>  val x = new X
>  x.y
>  x.z
>  x....

That's interesting, it's never occurred to me to do it that way because
I always avoid being in constructors if I can help it.  In case you are
not aware you can do what I generally do:

val x = new X
import x._
y
z
...

It's not zero boilerplate but I doubt you will gain much traction for
special casing this.  It does fall in the same oft-wished-for category
of wanting some way to have the contents of things imported without an
additional import statement, i.e.

def f[T: Numeric](x: T, y: T) = {
  // now if I want to be able to say "x < y" I have two statements
  val num = implicitly[Numeric[T]]
  import num._
  ...
}

> In the other hand the code that is naturally hierarchical (GUI
> building for instance) will not generate bunch of unnecessary
> anonymous classes - and this is very widespread use of such
> "initialization syntax".

GUI building was a motivation here too:

  http://drmaciver.com/ImportSIP.html

--
Paul Phillips      | All men are frauds.  The only difference between
Apatheist          | them is that some admit it.  I myself deny it.
Empiricist         |     -- H. L. Mencken
all hip pupils!    |----------* http://www.improving.org/paulp/ *----------
Reply | Threaded
Open this post in threaded view
|

Re: Compiler should avoid generating unnecessary anon classes

Vladimir Kirichenko-2
> In case you are
> not aware you can do what I generally do:
>
> val x = new X
> import x._
> y
> z
> ...

to make it expression, which is essential, it goes like:
{
  val x = new X
  import x._
  y
  z ( {
    val a = new A
    import a._
    b
    a
  })
  x
}

so there are disadvantages comparing to:

new X {
  y
  z ( new A { b })
}

Anyway proposed change is just useful optimization for generated code
size, clarity of stacktraces, etc. Anonymous classes are just
unnecessary here.

> GUI building was a motivation here too:
>  http://drmaciver.com/ImportSIP.html

This one is good, especially for DSL purposes, but in that case you
should be able to control the declaration side.

--
Best Regards,
Vladimir Kirichenko