2010-07-06 47 views

回答

19

你必須在MEAP從Debasish戈什的良好來源(早期訪問)一書
Gosh Cover
DSL in action(博客:「Ruminations of a programmer

scalatest測試框架是DSL的經典例子:

test("pop is invoked on an empty stack") {  
    val emptyStack = new Stack[String] 
    evaluating { emptyStack.pop() } should produce [NoSuchElementException] 
    emptyStack should be ('empty) 
    } 

還有很多其他基於DSL的框架:

  • specs: 「行爲驅動,設計框架」

  • internal DSLs

  • Squeryl: 「Scala的ORM和DSL的與最低的詳細程度和最大的類型安全數據庫會說話」

 
    def songCountByArtistId: Query[GroupWithMeasures[Long,Long]] = 
     from(artists, songs)((a,s) => 
     where(a.id === s.artistId) 
     groupBy(a.id) 
     compute(count) 
    ) 
+0

真的很好的書。還包括其他語言,以便您可以比較DSL在不同語言中的感受。 – 2010-08-11 19:31:25

+0

優秀的書。 Ghosh先生特別好地介紹了Scala(這是我閱讀的部分)。另外,您可能想要閱讀Spiewak先生關於Parser Combinators(對外部Scala DSLs至關重要)的教育,寫作良好的文章 - www.codecommit.com/blog/scala/the-magic-behind-parser-combinators – 2014-01-14 18:47:18

6

lift-json提供了一個DSL來生成JSON。例如,下面的DSL:

("person" -> 
    ("name" -> "Joe") ~ 
    ("age" -> 35) ~ 
    ("spouse" -> 
    ("person" -> 
     ("name" -> "Marilyn") ~ 
     ("age" -> 33) 
    ) 
) 
) 

創建以下JSON:

{ 
    "person": { 
    "name": "Joe", 
    "age": 35, 
    "spouse": { 
     "person": { 
     "name": "Marilyn", 
     "age": 33 
     } 
    } 
    } 
} 
1

兩個很好的例子是解析器組合和演員內置的DSL。有一個叫做DBC的SQL包裝器(尚未準備就緒),在這裏你可以看到它是怎樣的:http://scala.sygneca.com/libs/dbc

0

ScalaQL論文(PDF)描述了一個有趣的DSL語言集成數據庫查詢的實現。