加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Scala中创建内部DSL?

发布时间:2020-12-16 09:11:18 所属栏目:安全 来源:网络整理
导读:我一直在看一些关于域特定语言的书籍和资源. 我想我想在 Scala中构建一个内部的DSL. def instrument = new FXInstrument { provider = "EuroBase" instrumentOrders = List( new FXOrder { baseCcy = "GBP" termCcy = "EUR" legs = List( new FXLeg { amount
我一直在看一些关于域特定语言的书籍和资源.
我想我想在 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,因此它使用trait Proxy(mixin)

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

同上

instrument.orders(0).peer
???instrument.orders(0).legs(0).peer
???instrument.orders(0).legs(0).requirements(0).peer

等等.

我意识到领域特定的语言不像我想象的那么简单,但是上面提到的一些指针将是非常有用的.我会感谢你的回应. TA!

PP

解决方法

也许,这可以帮助你: DSL in Scala

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读