【问题标题】:Scala: How to override implicit constructor parameters?Scala:如何覆盖隐式构造函数参数?
【发布时间】:2016-09-20 19:25:23
【问题描述】:

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

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

    }
}

【问题讨论】:

  • 使用隐式方法!

标签: android scala implicit-parameters


【解决方案1】:

自我类型:

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

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

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2018-03-15
    • 2014-04-11
    • 2011-10-13
    • 2018-04-04
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多