17
BUILDING DSL S IN GROOVY speaker.identity { name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer', 'Author', 'Trainer' blog 'http://agiledeveloper.com/blog' email '[email protected]' }

Venkat Subramaniam Building DSLs In Groovy

  • Upload
    deimos

  • View
    3.181

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Venkat Subramaniam Building DSLs In Groovy

BUILDING DSLS IN GROOVY

speaker.identity { name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer', 'Author', 'Trainer' blog 'http://agiledeveloper.com/blog' email '[email protected]'}

Page 2: Venkat Subramaniam Building DSLs In Groovy

Abstract

Groovy's dynamic and meta programming capabilities make it a good candidate for implementing DSLs on the JVM. In this presentation we will take a look at a number of examples of DSLs implemented in Groovy.

We will discuss the significance of these DSLs, present other ideas for DSLs use case, and take an look at ways to implement them.

2

Page 3: Venkat Subramaniam Building DSLs In Groovy

Domain Specific Language

What’s DSL?

A language targeted at a particular type of problem

Syntax focuses on intended domain/problem

Unlike a general purpose language, you can’t use it for all kinds of stuff (or don’t want to)

Limited in scope and capability

Hence, they’re small and simple

3

Page 4: Venkat Subramaniam Building DSLs In Groovy

Characteristics of a DSL

What are some of the qualities of a DSL?

Context Driven

Extremely context sensitive

Fluent Interfaces

Provides for a humane, easy to understand interface

4

Page 5: Venkat Subramaniam Building DSLs In Groovy

Example of Context

5

Routed to context object lst

Page 6: Venkat Subramaniam Building DSLs In Groovy

Example of Fluency

6

Page 7: Venkat Subramaniam Building DSLs In Groovy

Examples of Groovy DSLs

7

Page 8: Venkat Subramaniam Building DSLs In Groovy

Examples of Groovy DSLs

8

Gant is Ant without XML

Built on top of Ant

Pure Groovy syntax to express builds

Page 9: Venkat Subramaniam Building DSLs In Groovy

Examples of Groovy DSLs

9

easyb is a Behavior Driven Design tool to express executable documentation of stories and specs

Uses Groovy DSLs and is highly expressive

Here’s story extracted from above “code”/DSL

Page 10: Venkat Subramaniam Building DSLs In Groovy

Examples of Groovy DSLs

10

Groovy’s XML Buildermakes building XML

tolerable

Page 11: Venkat Subramaniam Building DSLs In Groovy

Groovy Tricks to Build DSL

11

Take advantage of closures and delegates

Page 12: Venkat Subramaniam Building DSLs In Groovy

Groovy Tricks to Build DSL

12

Take advantage of dynamic methods

scores.dsl

process.groovy

Page 13: Venkat Subramaniam Building DSLs In Groovy

Groovy Tricks to Build DSL

13

Make Use of Categories

Type conversions------------------------2 => int.days => int.ago => Calendar.at => Date

Page 14: Venkat Subramaniam Building DSLs In Groovy

Groovy Tricks to Build DSL

14

Make Use of ExpandoMetaClass

Page 15: Venkat Subramaniam Building DSLs In Groovy

Groovy Tricks to Build DSL

15

Make Use of ExpandoMetaClass

Page 16: Venkat Subramaniam Building DSLs In Groovy

Referenceshttp://groovy.codehaus.org

http://docs.codehaus.org/display/GROOVY/Writing+Domain-Specific+Languages

http://docs.codehaus.org/display/GROOVY/Gant

http://www.easyb.org

“Programming Groovy: Dynamic Productivity for the Java Developer,” by Venkat Subramaniam, Pragmatic Bookshelf, 2008.

16

You can download examples and slides fromhttp://www.agiledeveloper.com - download

Page 17: Venkat Subramaniam Building DSLs In Groovy

Thank You!Please fill in your session evaluations

17

You can download examples and slides fromhttp://www.agiledeveloper.com - download