Back to directory
ThoughtWorksInc avatar

Dsl.scala

A framework to create embedded Domain-Specific Languages in Scala. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/ThoughtWorksInc/Dsl.scala.git

256

Stars

29

Forks

23

Watchers

Apache-2.0

License

Dsl.scala ThoughtWorks

Scala CI Scaladex Maven Central Scaladoc Join the chat at https://gitter.im/ThoughtWorksInc/Dsl.scala

Dsl.scala is a framework to create embedded Domain-Specific Languages in Scala. It can be considered as an alternative syntax to for comprehension, Scala Async and Scala Continuations. It unifies monads, generators, asynchronous functions, coroutines and continuations to a single universal syntax, and can be easily integrate to Scalaz, Cats, Scala Collections, Scala Futures, Akka HTTP, Java NIO, or your custom domains.

A DSL author is able to create language keywords by implementing the Dsl trait, which contains only one abstract method to be implemented. 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 Await keyword for creating memoized asynchronous values as Scala Futures, similar to the await / async keywords in C#, Python and JavaScript.
  • The Shift keyword for creating asynchronous tasks as delimited continuations, similar to the shift operator in Scala Continuations.
  • The AsynchronousIo.Connect, AsynchronousIo.Accept, AsynchronousIo.Read and AsynchronousIo.Write keywords for performing I/O on an asynchronous channel.
  • The Yield keyword for generating lazy streams, similar to yield in C#, Python and JavaScript.
  • The Fork keyword for duplicating current context, similar to the fork system call in POSIX.
  • The Return keyword for early returning, similar to the native return keyword in Scala.
  • The Using keyword to automatically close resources when exiting a scope, similar to the native using keyword in C#.
  • The Monadic keyword for creating Scalaz or Cats monadic control flow, similar to the !-notation in Idris.
  • The NullSafe keyword for the null safe operator, similar to the ? operator in Kotlin and Groovy.
  • The NoneSafe keyword for the None safe operator, similar to the Maybe monad in Haskell.

All the above keywords can be used together with each others. For example you can perform list comprehension to manipulate native resources in an asynchronous task by using Each, Using and Shift together.

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:

// Add the "keywords-yield" library in your build.sbt, to use the `Yield` keyword
libraryDependencies += "com.thoughtworks.dsl" %% "keywords-yield" % "latest.release"

// Add other "keywords-xxx" libraries in your build.sbt, to use other keywords
// libraryDependencies += "com.thoughtworks.dsl" %% "keywords-xxx" % "latest.release"

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

import com.thoughtworks.dsl.keywords.Yield
// Must not annotated with @tailrec
def xorshiftRandomGenerator(seed: Int): LazyList[Int] = reset {
  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 plain case class. You need a ! prefix to the keyword to activate the DSL.

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

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.

Showcases

(Feel free to add your project here)

Releases

Jan 26, 2023

Download .zip

What's Changed

Update scalafmt-core to 3.2.1 by @scala-steward in #479 Revert "Update scalafmt-core to 3.2.1" by @Atry in #480 Move com.thoughtworks.dsl.domains.task.Task to com.thoughtworks.dsl.doma...

v2.0.0-M2

Pre-release

Dec 7, 2021

Download .zip

What's Changed Port Dsl.scala 1.x keywords and domains to 2.x Full Changelog: v2.0.0-M1...v2.0.0-M2

Nov 26, 2021

Download .zip

What's Changed

Update scala-compiler, scala-library, ... to 2.12.15 by @scala-steward in #452 Update scalafmt-core to 3.1.1 by @scala-steward in #466 Update scalatest to 3.2.10 by @scala-steward in #...

v2.0.0-M1

Pre-release

Nov 26, 2021

Download .zip

What's Changed

A new implementation based on Scala 3 macros and opaque types Drop the support for Scala 2

Nov 12, 2021

Download .zip

What's Changed

Update monix to 3.2.0 by @scala-steward in #368 Update monix to 3.2.1 by @scala-steward in #370 Update akka-http to 10.1.12 by @scala-steward in #375 Update scalafmt-core to 2.6.3 by @...