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

Fwd: Re: compiler bug?

2 replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
i see. thx.

-------- Original-Nachricht -------- Betreff: Re: [scala-user] compiler bug? Datum: Thu, 12 Jan 2012 10:04:16 -0800 Von: Paul Phillips paulp [at] improving [dot] org (<paulp [at] improving [dot] org>) An: Dennis Haupt h-star [at] gmx [dot] de (<h-star [at] gmx [dot] de>)



On Thu, Jan 12, 2012 at 12:13 AM, Dennis Haupt <h-star [at] gmx [dot] de" rel="nofollow">h-star [at] gmx [dot] de> wrote:
did you run the code? the results are 1 and 3, never 5. that cannot be explained with shadowing.

It can, actually.  The 1 result comes from the companion of ImplicitResolutionOne.  The implicit which had been imported is shadowed by the definition of parse which follows it -- even though that definition is ineligible as an implicit, it shadows the name throughout the block.  The 3 result is because the implicit from ImplicitResolutionTwo is visible by its simple name, and so the companion object is never searched.
Leonid Vygovskiy
Joined: 2012-01-16,
User offline. Last seen 42 years 45 weeks ago.
Compiler bug?

I have next object:

object MyAlg {
val METRICS = "metrics"
}

class MyAlg {
def addMetrics(node: Elementt): NodeMetrics = {
var data = NodeMetrics(node)
if (data == null) {
data = new NodeMetrics
}
node.setUserData(CoreEx.METRICS, data, null)
return data;
}
}

That works nice now.

But my first version was:

object MyAlg extends App { // extends App is key difference
val METRICS = "metrics"
}

and code nice worked as Scala application, but thrown
NullPointerException when I used it from Java web application (Scala
part is included in application as library)
node.setUserData(MyAlg.METRICS, data, null)
with null as MyAlg.METRICS.

I think this is bug in Scala compiler.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Compiler bug?

On Mon, Jan 16, 2012 at 17:09, Leonid Vygovskiy
wrote:
> I have next object:
>
> object MyAlg {
>  val METRICS = "metrics"
> }
>
> class MyAlg {
>  def addMetrics(node: Elementt): NodeMetrics = {
>    var data = NodeMetrics(node)
>    if (data == null) {
>      data = new NodeMetrics
>    }
>    node.setUserData(CoreEx.METRICS, data, null)
>    return data;
>  }
> }
>
> That works nice now.
>
> But my first version was:
>
> object MyAlg extends App { // extends App is key difference
>  val METRICS = "metrics"
> }
>
> and code nice worked as Scala application, but thrown
> NullPointerException when I used it from Java web application  (Scala
> part is included in application as library)
>    node.setUserData(MyAlg.METRICS, data, null)
> with null as MyAlg.METRICS.
>
> I think this is bug in Scala compiler.

It isn't. When you extend App, you place all of the "constructor" code
inside a function that will be called by "main". So, when you use from
Java, main isn't called and METRICS isn't initialized.

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