Interesting DSLs, Implemented in Scala? [closed]

让人想犯罪 __ 提交于 2019-11-29 23:12:43

You have a good source in the MEAP (Early Access) book


DSL in action from Debasish Ghosh (blog: "Ruminations of a programmer)

Testing frameworks like scalatest are classic examples of DSL:

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

There are many others DSL-based frameworks out there:

  • specs: "Behaviour-Driven-Design framework"

  • internal DSLs

  • Squeryl: "A Scala ORM and DSL for talking with Databases with minimum verbosity and maximum type safety"

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

lift-json provides a DSL to generate JSON. For example the following DSL:

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

creates the following JSON:

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

ScalaModules is a DSL for working with OSGi.

Another one is available with Apache Camel a platform for enterprise integration.

Scala-Query and Squeryl also provide DSLs for querying databases among other things.

ScalaTest is also an awesome example of what is possible.

Two good examples are the built-in DSLs for Parser Combinators and Actors. There is a SQL wrapper called DBC (not yet ready), here you can see how it looks like: http://scala.sygneca.com/libs/dbc

XML in Scala is another example.

The ScalaQL paper (PDF) describes the implementation of an interesting DSL for language integrated database queries.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!