gpt4 book ai didi

scala - 如何在 Scala 中创建内部 DSL?

转载 作者:行者123 更新时间:2023-12-03 02:46:36 27 4
gpt4 key购买 nike

我一直在查看一些有关特定领域语言的书籍和资源。我想我想在 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) new FXOrder() {/闭包/}

我喜欢这种语法,它好吗,或者我应该更喜欢伴随对象。例如,我可以轻松快速地介绍其他 FX 类型。

2) 我想使用“peers”,例如 FXOrder 是一个 scala.Proxy mixee,因此它使用特征 Proxy (mixin)

例如“instrument.peer”给出了第三方专有API的内部对等Java对象(一个众所周知的金融服务交易系统,你能猜到吗?)

同上

instrument.orders(0).peer 仪器.订单(0).legs(0).peer Instrument.orders(0).legs(0).requirements(0).peer

等等。

我意识到领域特定语言并不像我想象的那么简单,但是上面的一些指示确实很有用。我将不胜感激您的回复。塔!

PP

最佳答案

我还没有考虑过你想要什么,但我看到了一个问题:

1) new FXOrder() { /closure/ }

不,事情不是这样的。当您使用 block 进行初始化(new Something)时,您正在执行匿名子类化。您实际上所做的是 new FXOrder() {/constructor,methods, getters and setters/}

关于scala - 如何在 Scala 中创建内部 DSL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com