2010-10-05 42 views
12

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

+1

您可以查看http://programming-scala.labs.oreilly.com/ch11.html#InternalDSLs – 2010-10-05 07:07:08

回答

6

也許,這可以幫助你:DSL in Scala

+0

此鏈接已損壞。 :( – Kao 2015-08-20 18:45:28

+0

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

5

我還沒有考慮你想要什麼,但我看到了一個問題:

1)新FXOrder( ){/ closure /}

不,它不會那樣工作。當您使用塊進行初始化(new Something)時,您正在執行匿名子類化。你實際上在做什麼是new FXOrder() { /constructor, methods, getters and setters/ }