本文介绍了在Scala中实现了有趣的DSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过(博客: )



测试框架如是DSL的经典示例:

 测试(在空堆栈上调用pop ){
val emptyStack = new Stack [String]
评估{emptyStack.pop()}应该产生[NoSuchElementException]
emptyStack应该是('empty)
}

还有很多其他基于DSL的框架:




  • :行为驱动的设计框架



  • :一种Scala ORM和DSL,用于以最小的冗长程度和最大的类型安全性与数据库进行对话




 
def songCountByArtistId:Query [GroupWithMeasures [Long,Long]] =
来自(艺术家,歌曲)((a,s] =>
其中(a.id === s.artistId)
groupBy(a.id)
compute(count)


I've seen BASIC and Apache Camel DSLs in Scala, and they're just fantastic. Any more examples of such DSLs?

解决方案

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)
      )

这篇关于在Scala中实现了有趣的DSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:27