- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Fwd: Re: compiler bug?
Thu, 2012-01-12, 19:14
i see. thx.
-------- Original-Nachricht -------- Betreff: Re: [scala-user] compiler bug? Datum: Thu, 12 Jan 2012 10:04:16 -0800 Von: Paul Phillips
An:
Dennis Haupt
On Thu, Jan 12, 2012 at 12:13 AM, Dennis Haupt <h-star [at] gmx [dot] de" rel="nofollow">h-star@gmx.de> wrote:
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.
-------- Original-Nachricht -------- Betreff: Re: [scala-user] compiler bug? Datum: Thu, 12 Jan 2012 10:04:16 -0800 Von: Paul Phillips
On Thu, Jan 12, 2012 at 12:13 AM, Dennis Haupt <h-star [at] gmx [dot] de" rel="nofollow">h-star@gmx.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.
Mon, 2012-01-16, 23:11
#2
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.
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.