# Announcing Dsl.scala, a simple framework for creating embedded DSL in Scala control flow

**URL:** <https://users.scala-lang.org/t/announcing-dsl-scala-a-simple-framework-for-creating-embedded-dsl-in-scala-control-flow/2425>\
**Category:** Announce\
**Created:** [March 8, 2018, 11:52am UTC](https://users.scala-lang.org/t/announcing-dsl-scala-a-simple-framework-for-creating-embedded-dsl-in-scala-control-flow/2425 "2018-03-08T11:52:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangbo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/yangbo/32/80_2.png) [@yangbo](https://users.scala-lang.org/u/yangbo)\
**Post date:** [March 8, 2018, 11:52am UTC](https://users.scala-lang.org/t/announcing-dsl-scala-a-simple-framework-for-creating-embedded-dsl-in-scala-control-flow/2425/1 "2018-03-08T11:52:32Z")

</div>

Happy Women’s Day everyone,

I just release [Dsl.scala](https://github.com/ThoughtWorksInc/Dsl.scala) 1.0.0-RC2, a simple framework for creating embedded **D** omain- **S** pecific **L** anguages in Scala control flow.

A DSL author is able to create language keywords by implementing the [Dsl](https://javadoc.io/page/com.thoughtworks.dsl/dsl_2.12/latest/com/thoughtworks/dsl/Dsl.html) trait, which contains only one simple function. No knowledge about Scala compiler or AST macros is required.

DSLs written in **Dsl.scala** are collaborative with others DSLs and Scala control flows. A DSL user can create functions that contains interleaved DSLs implemented by different vendors, along with ordinary Scala control flows.

We also provide some built-in keywords, including:

- The `Shift` keyword for asynchronous programming, similar to `await` / `async` in C#, Python and JavaScript.
- The `Yield` keyword for generating lazy streams, similar to `yield` in C#, Python and JavaScript.
- The `Fork` keyword for duplicating current thread, similar to the `fork` system call in POSIX.
- The `AutoClose` keyword to automatically close resources when exiting a scope, similar to the destructor feature in C++.
- The `Monadic` keyword for creating Scalaz or Cats monadic control flow, similar to the [!-notation in Idris](http://docs.idris-lang.org/en/latest/tutorial/interfaces.html#notation).

All the above keywords can be used together with each others.

## Getting Started

Suppose you want to create a random number generator. The generated numbers should be stored in a lazily evaluated infinite stream, which can be built with the help of our built-in domain-specific keyword `Yield`.

So, you need to add the library that contains the implementation of the keyword `Yield`:

```auto
// Add the following setting in your build.sbt 
libraryDependencies += "com.thoughtworks.dsl" %% "keywords-yield" % "1.0.0-RC2"

```

And the Dsl.scala compiler plug-ins that are shared by all DSLs:

```auto
// Add the following settings in your build.sbt 
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-bangnotation" % "1.0.0-RC2")
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-reseteverywhere" % "1.0.0-RC2")

```

See [MVNRepository](http://mvnrepository.com/artifact/com.thoughtworks.dsl?sort=newest) or [Scaladex](https://index.scala-lang.org/thoughtworksinc/dsl.scala) for the settings of other built-in DSLs for your build tools.

The random number generator which can be implemented as a recursive function that produce the next random number in each iteration.

```auto
import com.thoughtworks.dsl.instructions.Yield
def xorshiftRandomGenerator(seed: Int): Stream[Int] = {
  val tmp1 = seed ^ (seed << 13)
  val tmp2 = tmp1 ^ (tmp1 >>> 17)
  val tmp3 = tmp2 ^ (tmp2 << 5)
  !Yield(tmp3)
  xorshiftRandomGenerator(tmp3)
}

```

Note that a keyword is a simply case class. You need a `!` prefix to the keyword to activate the DSL.

It’s done. We can test it in ScalaTest:

```auto
val myGenerator = xorshiftRandomGenerator(seed = 123)
myGenerator(0) should be(31682556)
myGenerator(1) should be(-276305998)
myGenerator(2) should be(2101636938)

```

The call to `xorshiftRandomGenerator` does not throw a `StackOverflowError` because the execution of `xorshiftRandomGenerator` will be paused at the keyword `Yield`, and it will be resumed when the caller is looking for the next number.

Our DSLs are efficient. I have not compared it with `scala.concurrent.Future` or other implementation of asynchronous tasks, but [a rough benchmark](https://github.com/ThoughtWorksInc/Dsl.scala/blob/4510719/benchmarks-TaskBenchmark/src/main/scala/com/thoughtworks/dsl/benchmarks/TaskBenchmark.scala) shows our DSL for asynchronous task performs 0.9x ~ 2x better than Monix’s `Task`. I will publish the complete report of benchmark in couple of days. Check the [Scaladoc](https://javadoc.io/page/com.thoughtworks.dsl/dsl_2.12/latest/com/thoughtworks/dsl/index.html) to find examples about using or creating DSLs.

---

<div class="post-metadata">

**Author:** ![yangbo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/yangbo/32/80_2.png) [@yangbo](https://users.scala-lang.org/u/yangbo)\
**Post date:** [May 8, 2018, 4:09pm UTC](https://users.scala-lang.org/t/announcing-dsl-scala-a-simple-framework-for-creating-embedded-dsl-in-scala-control-flow/2425/2 "2018-05-08T16:09:44Z")

</div>

The complete benchmark report can be found at [Benchmarks: Dsl.scala vs Monix vs Cats Effect vs Scalaz Concurrent vs Scala Async vs Scala Continuation  
 ](https://github.com/ThoughtWorksInc/Dsl.scala/wiki/Benchmarks:-Dsl.scala-vs-Monix-vs-Cats-Effect-vs-Scalaz-Concurrent-vs-Scala-Async-vs-Scala-Continuation).

When using a direct style DSL, our !-bang notation is the fastest implementation among for-comprehension, Scala Async, and Scala Continuation. Especially, when performing a complex task to manipulate collections, our !-notation can be 12.5 times faster than `for` comprehension when running in current thread, and more than 3.1 times faster when running in a thread pool, despite conciser syntax of we provided.
