Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Create an Internal DSL in Scala?
    text
    copied!<p>I have been looking at a couple of books and resources on domain specific languages. I think I want to build an internal DSL in Scala.</p> <pre><code>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" } ) } ) } } </code></pre> <p>Such that following asserts are valid:</p> <pre><code> 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" </code></pre> <p>I just do not know quite how to implement the domain specific language as an internal representation</p> <p>1) new FXOrder() { /<em>closure</em>/ }</p> <p>I like this syntax, is it good or should I prefer companion objects. For instance I can quickly introduce other FX types easy.</p> <p>2) I want to use "peers" such FXOrder is a scala.Proxy mixee, thus it uses the trait Proxy (mixin)</p> <p>For example ``instrument.peer'' gives the internal peer Java Object of the third party proprietary API (a well known financial services trading system, can you guess?)</p> <p>Ditto for </p> <p>instrument.orders(0).peer instrument.orders(0).legs(0).peer instrument.orders(0).legs(0).requirements(0).peer</p> <p>and so on.</p> <p>I realise that domain specific language is not as simple as I thought, however some pointers on this above, would be really be useful. I would appreciate your responses. Ta!</p> <p><em>PP</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload