DelayedInit or OnCreate, any solution?

Some sort of DelayedInit replacement for scope management:

import db.Connection
@main def main():Unit = {
  val c = new Connection()
  //It is less error prone because nobody can forget  to call method and do import. 
  //And it is more easy to write
  c.execute{new{ 
    setValue(2.ss) 
  }}
}
package db
class Statement(){
  var value:String = "default value"
  def execute():Unit = println(s"value=$value")
}
class StatementBuilder(given private val  s:Statement){
  //here it can be extensions, typeclasses 
  def (v:Int)ss: String = v.toString()
  def setValue(v:String):Unit = s.value = v 
}
class Connection{
  def execute(buildStatement:(given Statement)=>StatementBuilder): Unit = {
    //before advice
    given s:Statement = new Statement()
    //dsl statement
    buildStatement
    //after advice
    s.execute()
  }
}
1 Like