Search code examples
scalaconstructorpass-by-name

By-Name-Parameters for Constructors


coming from my other question is there a way to get by-name-parameters for constructors working? I need a way to provide a code-block which is executed on-demand/lazy/by-name inside an object and this code-block must be able to access the class-methods as if the code-block were part of the class.

Following Testcase fails:

package test

class ByNameCons(code: => Unit) {

    def exec() = {
        println("pre-code")
        code
        println("post-code")
    }

    def meth() = println("method")

    def exec2(code2: => Unit) = {
        println("pre-code")
        code2
        println("post-code")
    }
}


object ByNameCons {

    def main(args: Array[String]): Unit = {
        val tst = new ByNameCons {
            println("foo")
            meth() // knows meth() as code is part of ByNameCons
        }
        tst.exec() // ByName fails (executed right as constructor)


        println("--------")


        tst.exec2 { // ByName works
            println("foo")
            //meth() // does not know meth() as code is NOT part of ByNameCons
        }       
    }
}

Output:

foo
method
pre-code
post-code
--------
pre-code
foo
post-code

Solution

  • This is because when you're making an instance like this:

    val tst = new ByNameCons {
      ...
    }
    

    .. you're actually creating an anonymous class, like in java. The above code is the same as:

    val tst = new ByNameCons() { ... }
    

    .. while the correct syntax for passing by-name is:

    val tst = new ByNameCons( { ... } )
    

    You cant omit parentheses the same way for constructors as with functions.