我一直在尋找一些關於領域特定語言的書籍和資源。 我想我想在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