Skip to content

Commit

Permalink
Bonus test: builder pattern
Browse files Browse the repository at this point in the history
This shows that the builder pattern can be expressed
with implicit function types.
  • Loading branch information
odersky committed Feb 18, 2017
1 parent 144546d commit 93ad117
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Expand Up @@ -338,8 +338,9 @@ class TreeChecker extends Phase with SymTransformer {
val tpe = tree.typeOpt
val sym = tree.symbol
if (!tpe.isInstanceOf[WithFixedSym] &&
sym.exists && !sym.is(Private) &&
!tree.name.isOuterSelect // outer selects have effectively fixed symbols) {
sym.exists && !sym.is(Private) &&
!tree.name.isOuterSelect // outer selects have effectively fixed symbols
) {
val qualTpe = tree.qualifier.typeOpt
val member =
if (sym.is(Private)) qualTpe.member(tree.name)
Expand Down
1 change: 1 addition & 0 deletions tests/run/builder.check
@@ -0,0 +1 @@
Table(Row(Cell(A1), Cell(B1)), Row(Cell(A2), Cell(B2)))
51 changes: 51 additions & 0 deletions tests/run/builder.scala
@@ -0,0 +1,51 @@
import 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(", ", ", ")")
}

class Cell(elem: String) {
override def toString = s"Cell($elem)"
}

object Test {

def table(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))

val data =
table {
row {
cell("A1")
cell("B1")
}
row {
cell("A2")
cell("B2")
}
}

def main(args: Array[String]) = {
println(data)
}
}

0 comments on commit 93ad117

Please sign in to comment.