Scala:如何覆盖隐式构造函数参数?

时间:2016-09-20 19:25:23

标签: android scala implicit-parameters

我目前正在开发一款针对Android的小scala DSL(https://github.com/bertderbecker/scalandroid)。

val drawerLayout = new SDrawerLayout {
    openDrawerFrom = SGravity.LEFT
    fitsSystemWindows = true

    navigationView = new SNavigationView {
        println(parent)                           //None - the parent of the drawerlayout
                                                            // I want that the parent is the drawerlayout 
        layout = SLayout.NAVI_HEADER_DEFAULT
        fitsSystemWindows = true
    }
}

如何告诉SNavigationView使用SDrawerLayout作为其父级,而不是DrawerLayout的父级?

所以,更一般地说,我想要一个类foo(0),它将foo(-1)作为一个隐式参数,你可以定义一个foo(+ 1),它将取foo(0) )从一开始就作为隐含参数。

所以foo是“递归的”。

我想要的是:

class foo()(implicit parent: foo) {
  parent = this
  val f = new Foo {      //takes this as its parent

    }
}

1 个答案:

答案 0 :(得分:0)

自我类型:

val drawerLayout = new SDrawerLayout {drawer =>
  openDrawerFrom = SGravity.LEFT
  fitsSystemWindows = true

  navigationView = new SNavigationView {
    println(drawer.parent)                           //None
    layout = SLayout.NAVI_HEADER_DEFAULT
    fitsSystemWindows = true
  }
}