Skip to content

Comments

Prevent initialization bugs#2

Open
b-studios wants to merge 1 commit intoklauso:masterfrom
b-studios:patch-2
Open

Prevent initialization bugs#2
b-studios wants to merge 1 commit intoklauso:masterfrom
b-studios:patch-2

Conversation

@b-studios
Copy link
Contributor

I am not sure whether this is important enough to be included in the lecture notes, but using val here can cause initialization problems (access to symMap to early causes null pointer exception).

The problem occurred when executing

trait MyLanguage extends Integers with Convs
trait MyLanguageExp extends MyLanguage with IntegersExp with ConvsExp
trait testProgram { self: MyLanguage =>
  val x: Rep[Int] = lift(42) + lift(1) * 3 
}
object test extends testProgram with MyLanguageExp
java.lang.NullPointerException
    at BaseExp$class.findOrCreateDefinition
    at test$.findOrCreateDefinition
    at BaseExp$class.toAtom
    at test$.toAtom
    at IntegersExp$class.mult

Instead using lazy val fixes the problem, since it provide means for initialization order independence.

I am not sure whether this is important enough to be included in the lecture notes, but using `val` here can cause initialization problems (access to `symMap` causes `null` pointer exception).

The problem occurred when executing

~~~scala
trait MyLanguage extends Integers with Convs
trait MyLanguageExp extends MyLanguage with IntegersExp with ConvsExp
trait testProgram { self: MyLanguage =>
  val x: Rep[Int] = lift(42) + lift(1) * 3 
}
object test extends testProgram with MyLanguageExp
~~~

~~~text
java.lang.NullPointerException
    at BaseExp$class.findOrCreateDefinition
    at test$.findOrCreateDefinition
    at BaseExp$class.toAtom
    at test$.toAtom
    at IntegersExp$class.mult
~~~

Instead using `lazy val` fixes the problem, since it provide means for initialization order independence.
@Blaisorblade
Copy link
Collaborator

Thanks for submitting a pull request.

But I wonder: wouldn't

object test extends MyLanguageExp with testProgram

or some variation avoid the problem?
But whether the code should use lazy val to be more robust anyway is still a good question in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants