我一直在寻找一些关于领域特定语言的书籍和资源。 我想我想在Scala中构建一个内部DSL。如何在Scala中创建内部DSL?
def instrument = new FXInstrument {
provider = "EuroBase"
instrumentOrders = List(
new FXOrder {
baseCcy = "GBP"
termCcy = "EUR"
legs = List(
new FXLeg {
amountPrice = 100000.0
spotPrice = 1.56
requirements = List(
new FXRequirement {
baseCcy="GBP" termCcy="EUR"
settlement="Banker Rain"
}
)
},
new FXLeg {
amountPrice = 200000.0
spotPrice = 1.50
requirements = List(
new FXRequirement {
baseCcy="GBP" termCcy="EUR"
settlement="Banker Sunny"
}
)
}
)
}
}
,使得下面的断言是有效的:
instrument.orders(0).baseCcy should equal ("GBP")
instrument.orders(0).termCcy should equal ("EUR")
instrument.orders(0).legs(0).amountPrice should equal 100000.0
instrument.orders(0).legs(0).spotPrice should equal 1.56
instrument.orders(0).legs(1).amountPrice should equal 200000.0
instrument.orders(0).legs(1).spotPrice should equal 1.50
instrument.orders(0).legs(0).requirements(0).settlement should equal "Banker Rain"
instrument.orders(0).legs(1).requirements(0).settlement should equal "Banker Sunny"
我只是不知道怎么和实施领域特定语言作为内部表示
1)新FXOrder(){/ closure /}
我喜欢这种语法,它是好还是我应该喜欢伴侣对象。例如,我可以很容易地快速引入其他FX类型。
2)我想使用“对等体”这样FXOrder是scala.Proxy mixee,从而它使用性状代理(混入)
例如``instrument.peer“”给出了内部对等体Java对象第三方私有的API的(众所周知的金融服务交易系统,你可以猜到的?)
同上,用于
instrument.orders(0).peer instrument.orders(0).legs(0) .peer instrument.orders(0).legs(0).requirements(0).peer
等等。
我意识到领域特定的语言并不像我想象的那么简单,但是上面的一些指针会非常有用。我会很感激你的回应。 TA!
PP
您可以查看http://programming-scala.labs.oreilly.com/ch11.html#InternalDSLs – 2010-10-05 07:07:08