Apache Groovy is a dynamic language, for Java platform, aimed at improving developer productivity with familiar and easy to learn syntax. It's meta programming capabilities makes it a good candidate for DSL.
Domain Specific Languages (DSLs) have become a valuable part of the Groovy idiom. DSLs are used in native Groovy builders, Grails and GORM, and testing frameworks. To a developer, DSLs are consumable and understandable, which makes implementation more fluid compared to traditional programming. But how is a DSL implemented? How does it work behind the scenes?
2. www.tothenew.com
What is DSL?
● A computer language specialized to a particular application domain
● Not intended to be broadly applicable across domains
● Syntax is focused on intended domain or problem
● Small and simple
3. www.tothenew.com
About me
speaker {
name 'Puneet Behl'
organization 'TO THE NEW'
designation 'Associate Lead - Technology'
email 'puneet.behl@tothenew.com'
twitter '@puneetbhl'
github 'http://github.com/puneetbehl'
}
9. www.tothenew.com
Examples - Spock
class AdderSpec extends spock.lang.Specification {
def "adder-test"() {
given: "a new Adder class is created"
def adder = new Adder();
expect: "Adding two numbers to return the sum"
adder.add(3, 4) == 7
}
}
14. www.tothenew.com
Conference.cfp {
title "Building DSL using Groovy"
description """
Domain Specific Languages (DSLs) have become a valuable part of
the Groovy idiom. DSLs are used in native Groovy builders, Grails and
GORM, and testing frameworks. To a developer, DSLs are consumable and
understandable, which makes implementation more fluid compared to
traditional programming. But how is a DSL implemented? How does it work
behind the scenes?
In this talk, we will talk about DSL and how can we write DSL
using Groovy with various examples and see the significance of DSL
"""
level INTERMEDIATE
owner "Puneet Behl"
duration "50.mins"
}
Closure and Delegates
A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains. There is a wide variety of DSLs, ranging from widely used languages for common domains, such as HTML for web pages, down to languages used by only one or a few pieces of software, such as Emacs Lisp for GNU Emacs and XEmacs. DSLs can be further subdivided by the kind of language, and include domain-specific markup languages, domain-specific modelinglanguages (more generally, specification languages), and domain-specific programming languages. Special-purpose computer languages have always existed in the computer age, but the term "domain-specific language" has become more popular due to the rise of domain-specific modeling. Simpler DSLs, particularly ones used by a single application, are sometimes informally called mini-languages.
https://softwareengineering.stackexchange.com/questions/80389/can-css-be-considered-a-dsl
We all know the English meaning of the word fart, but did you know that fartmeans speed in Danish, Norwegian, and Swedish? If that doesn’t make you snicker enough, the words for speed bump in each language are fartbump, fartshump, and farthinder.
The code is written in a behaviour style, which is closer to English than to programming language code
The class has clear sections annotated by labels (given: and expect:) that clearly identify the purpose of the code statements they contain
Each label has a comment in the same line that provides a brief description of the business logic
There are no “assert” methods (like JUnit). The code is much more compact.
The code is written in a behaviour style, which is closer to English than to programming language code
The class has clear sections annotated by labels (given: and expect:) that clearly identify the purpose of the code statements they contain
Each label has a comment in the same line that provides a brief description of the business logic
There are no “assert” methods (like JUnit). The code is much more compact.
show = { println it }
square_root = { Math.sqrt(it) }
def please(action) {
[the: { what ->
[of: { n -> action(what(n)) }]
}]
}
@TypeChecked - type checking
@DelegatesTo - goal of this annotation is to solve both the documentation issue, that will let your IDE know about the expected methods in the closure body, and it will also solve the type checking issue, by giving hints to the compiler about what are the potential receivers of method calls in the closure body.
The Script class provides base support for integration with your application through the Binding object, as illustrated in this example:
Whether you are using groovyc to compile classes or a GroovyShell, for example, to execute scripts, under the hood, a compiler configuration is used. This configuration holds information like the source encoding or the classpath but it can also be used to perform more operations like adding imports by default, applying AST transformations transparently or disabling global AST transformations.