Dsl.scala is a framework to create embedded Domain-Specific Languages in Scala.
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 theawait
/async
keywords in C#, Python and JavaScript. - The
Shift
keyword for creating asynchronous tasks as delimited continuations, similar to theshift
operator in Scala Continuations. - The
AsynchronousIo.Connect
,AsynchronousIo.Accept
,AsynchronousIo.Read
andAsynchronousIo.Write
keyword for perform I/O on an asynchronous channel. - The
Yield
keyword for generating lazy streams, similar toyield
in C#, Python and JavaScript. - The
Each
keyword for iterating on a collection, similar to the list comprehension feature in Scala, Haskell, OCaml, Python and Lisp. - The
Continue
keyword LDK for skipping an element in aEach
collection comprehension, similar to the nativecontinue
keyword in C/C++ or themzero
in Haskell. - The
Fork
keyword for duplicating current context, similar to thefork
system call in POSIX. - The
Return
keyword for early returning, similar to the nativereturn
keyword in Scala. - The
Using
keyword to automatically close resources when exiting a scope, similar to the nativeusing
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 theNone
safe operator, similar to theMaybe
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
, AutoClose
and Shift
together.
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 following setting in your build.sbt
libraryDependencies += "com.thoughtworks.dsl" %% "keywords-yield" % "latest.release"
And the Dsl.scala compiler plug-ins that are shared by all DSLs:
// Add the following settings in your build.sbt
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-bangnotation" % "latest.release")
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-reseteverywhere" % "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.keys.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 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.
- sbt-ammonite-classpath is an sbt plug-in that uses
Each
keywords to iterate through configuations and keys, as an alternative syntax offor
comprehensions.
(Feel free to add your project here)
- Check the Documentation to find examples about using or creating DSLs.
- See MVNRepository or Scaladex for the settings of each built-in DSLs for your build tools.
- Benchmarks: Dsl.scala vs Monix vs Cats Effect vs Scalaz Concurrent vs Scala Async vs Scala Continuation