# DelayedInit or OnCreate, any solution?

**URL:** <https://contributors.scala-lang.org/t/delayedinit-or-oncreate-any-solution/1748>\
**Category:** Language Design\
**Created:** [March 28, 2018, 3:02pm UTC](https://contributors.scala-lang.org/t/delayedinit-or-oncreate-any-solution/1748 "2018-03-28T15:02:54Z")\
**Posts on this page:** 1\
**Showing post:** 29

<div class="post-metadata">

**Author:** ![AMatveev](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/amatveev/32/847_2.png) [@AMatveev](https://contributors.scala-lang.org/u/AMatveev)\
**Post date:** [August 29, 2018, 5:16pm UTC](https://contributors.scala-lang.org/t/delayedinit-or-oncreate-any-solution/1748/29 "2018-08-29T17:16:04Z")

</div>

Ok we have:  
p1\Main.scala

```auto
package p1
import scala.collection.mutable.ArrayBuffer

class Table {
  val rows = new ArrayBuffer[Row]
  def add(r: Row): Unit = rows += r
  override def toString = rows.mkString("Table(", ", ", ")")
}

class Row {
  val cells = new ArrayBuffer[Cell]
  def add(c: Cell): Unit = cells += c
  override def toString = cells.mkString("Row(", ", ", ")")
}

case class Cell(elem: String)

object HtmlTable{
    def htmlTable(init: implicit Table => Unit) = {
      implicit val t = new Table
      init
      t
    }

    def row(init: implicit Row => Unit)(implicit t: Table) = {
      implicit val r = new Row
      init
      t.add(r)
    }

    def cell(str: String)(implicit r: Row) =
      r.add(new Cell(str))
}

```

p3\Main.scala

```auto
package p3
import scala.collection.mutable.ArrayBuffer

class Table1 {
  val rows = new ArrayBuffer[Row1]
  def add(r: Row1): Unit = rows += r
  override def toString = rows.mkString("Table(", ", ", ")")
}

class Row1 {
  val cells = new ArrayBuffer[Cell1]
  def add(c: Cell1): Unit = cells += c
  override def toString = cells.mkString("Row(", ", ", ")")
}

case class Cell1(elem: String)

object ExcelTable{
    def excelTable(init: implicit Table1 => Unit) = {
      implicit val t = new Table1
      init
      t
    }

    def row(init: implicit Row1 => Unit)(implicit t: Table1) = {
      implicit val r = new Row1
      init
      t.add(r)
    }

    def cell(str: String)(implicit r: Row1) =
      r.add(new Cell1(str))
}

```

p2\main.scala

```auto
object Test{
   def main():Unit = {
       import p3.ExcelTable._ // it does not work without that 
       //import p1.Dsl.table // it is not enough
        excelTable {
          row {
            cell("top left")
            cell("top right")
          }
          row {
            cell("bottom left")
            cell("bottom right")
          }
        }
        //It does not work in this context 
        import p1.HtmlTable._  
        htmlTable {
          row {
            cell("top left")
            cell("top right")
          }
          row {
            cell("bottom left")
            cell("bottom right")
          }
        }        
   }
}

```

And we have compilation error

```auto
[info] Compiling 1 Scala source to C:\buf\dotty\sample\target\scala-0.8\classes ...
[error] -- [E049] Reference Error: C:\buf\dotty\sample\src\main\scala\p2\Main.scala:19:10
[error] 19 | row {
[error] | ^^^
[error] | reference to `row` is ambiguous
[error] | it is both imported by import p3.ExcelTable._
[error] | and imported subsequently by import p1.HtmlTable._
[error] -- [E049] Reference Error: C:\buf\dotty\sample\src\main\scala\p2\Main.scala:20:12
[error] 20 | cell("top left")

```

If we try to work with these tables simultaneously

---

_[View the full topic](https://contributors.scala-lang.org/t/delayedinit-or-oncreate-any-solution/1748)._
