What is DSL in Scala? [closed]

丶灬走出姿态 提交于 2019-12-03 08:33:59

As others have pointed out, the first part of the question ("what is a DSL?") is essentially answered by What is a DSL and where should I use it?

I'll try to answer instead to the second part: why are DSLs so popular in Scala?

The reason is that Scala (as opposed to other languages like Java) offers many syntactic facilities to provide DSLs.

For example, Scala has infix method applications:

someObject.someMethod(someArgument)

// can be written as

someObject someMethod someArgument

This makes introducing custom "operators" very easy in the language. A notable example is the akka DSL for sending messages to an actor:

actor ! message

which is a DSL mimicking the syntax of Erlang.

Another example of a syntactic facility in Scala is the "trailing block argument" (not sure it has a precise name):

def someMethod(x: Int)(y: String) = ???

// can be invoked as
someMethod(42)("foo")

// but also as
someMethod(42) { "foo" }

which is very interesting when the last parameter is a function:

def someOtherMethod[A, B](x: A)(f: A => B): B = ???

someOtherMethod(42) { a =>
  // ...a very long body
}

In other languages, blocks ({ ... }) are usually reserved to built-in control-flow structures (such as if, while, for, etc), but in Scala you can use this syntactic facility to build custom methods that resemble built-in control structures.

Those two features alone are distinctive enough for explaining why DSL are so pervasive in the Scala community.

Digging a bit deeper, we can also mention implicit conversions, which allow to add custom methods to any existing type. For example

implicit class TimesOps(x: Int) {
  def times(y: Int): Int = x * y
}

// then use as

2 times 4 // 8

This example combines the use of infix method application and implicit conversions.

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