2010-07-06 63 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语言集成数据库查询的实现。