Scala DSL - Nested block referencing parent

后端 未结 2 1437
慢半拍i
慢半拍i 2021-01-19 19:30

Playing about with a DSL in Scala, so lets say I have something like this:

house {
  floor {
    bedroom(\"kids)
    bedroom(\"master\")
  }
  floor {
    ki         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 20:20

    Do you really need each block to have a reference to the enclosing block? Or was it just so that you could add the nested block to the parent block? In this case you could simply pass nested blocks to the enclosing block, so to speak:

    house (
      floor (
        bedroom("kids"),
        bedroom("master")
      ),
      floor (
        kitchen
      )
    )
    

    Using the following definitions:

    trait HouseElement
    case class house( elements: HouseElement* )
    trait FloorElement
    case class floor( elements: FloorElement * ) extends HouseElement
    case class bedroom( name: String ) extends FloorElement
    case object kitchen extends FloorElement
    

    Otherwise, another solution is to rely heavily on anonymous classes (which unfortunately requires to use the new keyword everywhere):

    new house {
      new floor {
        new bedroom("kids")
        new bedroom("master")
      }
      new floor {
        new kitchen()
      }
    }
    

    Using the following definitions:

    import collection.mutable.Buffer
    class house {
      val elements = Buffer[Element]()
      trait Element {
        elements += this 
      }        
      class floor extends Element { 
        val elements = Buffer[Element]()
        trait Element {
          elements += this 
        }        
        class bedroom(name: String) extends Element 
        class kitchen extends Element
      }
    }
    

提交回复
热议问题