如何在Scala中创建内部DSL?
我一直在寻找一些关于领域特定语言的书籍和资源。 我想我想在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
也许,这可以帮助你:DSL in Scala
此链接已损坏。 :( – Kao 2015-08-20 18:45:28
https://www.amazon.com/Programming-Scala-Scalability-Functional-Objects/dp/0596155956%3FSubscriptionId%3DAKIAILSHYYTFIVPWUY6Q%26tag%3Dduckduckgo-d-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953 %26creativeASIN%3D0596155956 – adrian 2017-01-19 20:53:59
我还没有考虑你想要什么,但我看到了一个问题:
1)新FXOrder( ){/ closure /}
不,它不会那样工作。当您使用块进行初始化(new Something
)时,您正在执行匿名子类化。你实际上在做什么是new FXOrder() { /constructor, methods, getters and setters/ }
。
您可以查看http://programming-scala.labs.oreilly.com/ch11.html#InternalDSLs – 2010-10-05 07:07:08