Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is DSL in Scala? [closed]

Tags:

scala

dsl

Going through various Scala related material, term DSL is used at many places.

Google search tells it is Domain specific language.

What exactly it means, and why is it that this term doesn't comes across while learning other languages like Java?

like image 845
Mandroid Avatar asked Mar 11 '18 03:03

Mandroid


1 Answers

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.

like image 90
Gabriele Petronella Avatar answered Nov 09 '22 07:11

Gabriele Petronella