Skip to content

Instantly share code, notes, and snippets.

@b-studios
Last active May 22, 2017 15:52
Show Gist options
  • Save b-studios/f2310811c6bd1895a927ae089e074bba to your computer and use it in GitHub Desktop.
Save b-studios/f2310811c6bd1895a927ae089e074bba to your computer and use it in GitHub Desktop.
"full" code for example from Martin's talk on implicits
class Table(var rows: List[Row] = Nil) {
def add(r: Row) = { rows = r :: rows }
}
class Row(var cells: List[Cell] = Nil) {
def add(c: Cell) = { cells = c :: cells }
}
case class Cell(content: String)
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(Cell(str))
val doc = table {
row {
cell("test")
}
table {
row {
cell("test2")
}
}
}
}
// result of
// dotc -Xprint:posttyper examples/scoping.scala
// ...
val doc: Table =
test.table(
{
def $anonfun(implicit evidence$1: Table): Unit =
{
test.row(
{
def $anonfun(implicit evidence$2: Row): Unit =
{
test.cell("test")(evidence$2)
}
closure($anonfun)
}
)(evidence$1)
{
test.table(
{
def $anonfun(implicit evidence$3: Table): Unit =
{
test.row(
{
def $anonfun(implicit evidence$4: Row): Unit =
{
test.cell("test2")(evidence$4)
}
closure($anonfun)
}
)(evidence$3)
}
closure($anonfun)
}
)
()
}
}
closure($anonfun)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment