Scala: How to override implicit constructor parameters?

131 Views Asked by At

I am currently working on a little scala DSL for Android (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
    }
}

How can I tell the SNavigationView to use the SDrawerLayout as its parent, not the parent of the DrawerLayout?

So, more generally, I want to have a class foo (0) , which takes a foo (-1) as an implicit parameter and within you can define a foo (+1), which will take the foo (0) from the beginning as an implicit parameter.

So foo is "recursive".

What I want is:

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

    }
}
1

There are 1 best solutions below

0
Alvaro Carrasco On

Self types:

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

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