用于配置的编程语言

时间:2014-06-04 10:53:34

标签: java scala configuration programming-languages

我需要一个带有少量代码的配置语言。我试图使用scala。我可以使用Scala或其他JVM语言编写类似

的内容
... = car() {
    title = "Experimental Car"
    engine = dieselEngine() {
        capacity = 1.9
    }
}

而不是

val c = car()
c.title = "Experimental Car"
c.engine = c.dieselEngine()
c.engine.capacity = 1.9

重要的是,柴油发动机是汽车的方法。

3 个答案:

答案 0 :(得分:1)

Scala案例类提供了您所需的内容:

case class Car(title: String, engine: Engine)

// A marker trait and several implementations
sealed trait Engine
case class DieselEngine(capacity: Double) extends Engine
case class ElectricEngine(capacity: Int) extends Engine

val c = Car(
  title = "Experimental car",
  engine = DieselEngine(capacity = 1.9)
)

有关详情,请参阅here

答案 1 :(得分:1)

尝试Groovy方法with

... = new Car().with {
  title = "Experimental Car"
  engine = dieselEngine().with {
    capacity = 1.9
    return this
  }
  return this
}

答案 2 :(得分:0)

Groovy语言中的Builders也很好地支持了这一点。你有语法

car {
    title: "Experimental Car"
    engine: dieselEngine {
        capacity: 1.9
    }
}