Search code examples
scalanullpointerexceptioncircular-reference

Initialized object is null (due to circular dependence?)


I have a simple grammar

Expr -> Byte | Sum Expr Expr

and the code below is supposed to generate the random trees for it

object randomTree extends App {
    // Expr -> Byte | Sum Expr Expr
    def intRnd(len: Int, start: Int = 0): Int = (math.random * len toInt) + start
    def byteRnd = intRnd(256, -128)
    case class Value[A](value: A, parent: Type) extends Type {
        def gen = this
        override def toString = value + ":" + parent.getClass.getSimpleName
    }

    trait Type {
        def gen: Type //def gen[A]: Value[A]
        override def toString = getClass.getSimpleName
    }

    class OR/*Enum*/(alternatives: Type*) extends Type {
        def gen = alternatives(intRnd(alternatives.length)).gen
    }

    class AND/*Sequence*/(alternatives: Type*) extends Type {
        def gen = {
            println("Sum " + alternatives)// prints: Sum WrappedArray(null, null)
            Value(alternatives.map(_.gen), this)
        }
    }

    object Expr extends OR(Sum, Byte) {
        override def gen = Value(super.gen, this)
    }
    //object Sum extends Type { // everything is fine if this Sum is used
        //def gen = Value(Expr.gen -> Expr.gen, this) }
    println("Expr = " + Expr) // prints: Expr = Expr$
    object Sum extends AND(Expr, Expr) // this Sum causes NPE
    object Byte extends Type {
        def gen = Value(byteRnd, this)
    }
    (1 to 10) foreach { i=> println(Expr.gen) }

}

I wonder why object Sum extends AND(Expr, Expr) expands into AND(WrappedArray(null, null)) since Expr is a non-null object and how can I initialize the Expr such that Sum comes out right?


Solution

  • Since there is a circular reference between Expr and Sum that's caused null values, By Name Parameter can be used to solve this circular reference issue to delay the object's initialization. like:

    ...
    class OR /*Enum*/ (alternatives: => Array[Type]) extends Type {
    ...
    class AND /*Sequence*/ (alternatives: => Array[Type]) extends Type {
    ...
    

    In the above code: alternatives: => Array[Type] as the By Name parameter to delay the circular object initialization time to avoid the null values.