SlideShare a Scribd company logo
1 of 33
Max Bureck, 08. June 2016
XTEND – API AND DSL DESIGN PATTERNS
2
XTEND – API AND DSL DESIGN PATTERNS
Intro – Xtend
 Xtend is a general purpose programming language transpiling to Java source
 Its syntax is flexible allowing definition of internal DSLs and interesting APIs
 This presentation will show some ways how the syntax can be utilized
 No detailed explanation of Xtend‘s features though
© Fraunhofer FOKUS
©MatthiasHeyde/FraunhoferFOKUS©MatthiasHeyde/FraunhoferFOKUS
3
XTEND – API AND DSL DESIGN PATTERNS
Intro – Patterns
 Based on some observations from designing Xtend APIs
 Some ideas inspired by other languages (e.g. Scala, F#)
 Some patterns may or should be implemented via active annotations in future
© Fraunhofer FOKUS
©MatthiasHeyde/FraunhoferFOKUS©MatthiasHeyde/FraunhoferFOKUS
4
XTEND – API AND DSL DESIGN PATTERNS
Intro – The Tools Provided By Xtend
 Lambdas
 Call with lambda as last parameter: place after brackets; omit empty brackets
strProv.apply([String s | println(s)]) ⇨ strProv.apply [println(it)]
 Setter call can be written as assignment
button.setText("Press Me") ⇨ button.text = "Press Me"
 Extension methods
emphasize("boo") ⇨ "boo".emphasize
 Operator overloading
operator_plus(1e15bd, 1e-4bd) ⇨ 1e15bd + 1e-4bd
 Active annotations
© Fraunhofer FOKUS
©MatthiasHeyde/FraunhoferFOKUS©MatthiasHeyde/FraunhoferFOKUS
5
XTEND – API AND DSL DESIGN PATTERNS
Pattern Overview
 Nested Block Syntax
 Fluent Case Distinction
 Immutable Data Structure Patterns
 Implicit Parameter Values
 Type Providers
 API Xtendification
© Fraunhofer FOKUS
6
Nested Block Syntax, Use Case
 Lambda as last argument looks like a named block
 Can be exploited to create internal DSLs that look like nested blocks
 Declarative look, while being imperative
 Especially useful when building up object trees, e.g.
 UI elements
 Configuration
 Etc.
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
7
Nested Block Syntax, Callback API Example in Java 8
server( (conf) -> {
conf.setPort(80);
conf.get("/hello?name=$name", (response) -> {
response.header(Pair.of("content", "text/html"));
return HtmlBuilder.html(response, (builder) -> {
builder.h1("Hello " + builder.param("name"));
});
});
});
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
8
Nested Block Syntax, Callback API Example
server [
port = 80
get("/hello?name=$name") [
header("Content-Type" -> "text/html")
html [
h1("Hello " + param('name'))
]
]
]
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
Assignment to setter on default argument
default argument it
Implicit return of last expression result
Extension method
Method with lambda argument
Mapping operator
9
Nested Block Syntax, Summary
 Nested block APIs reflect logical containment structures in code
 Xtend reduces visual noise and enables declarative look
 Can improve maintainability due to clear intent and readability of code
 "Traditional" APIs may be used as nested blocks, using => operator
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
10
Fluent Case Distinction, Example: Object Decomposition in Java 8
ParentalStatus parentalStatus = bob.getParentalStatus();
if(parentalStatus instanceof Parents) {
Parents parents = (Parents) parentalStatus;
Optional<Person> momOpt = parents.getMom();
Optional<Person> dadOpt = parents.getDad();
momOpt.ifPresent((mom) -> dadOpt.ifPresent((dad) -> {
System.out.println("Mother: "+mom.getName()+", Father: "+ dad.getName());
}));
} else {
if(parentalStatus instanceof Orphan) {
String orphanage = ((Orphan) parentalStatus).getOrphanage();
System.out.println("Orphanage: "+orphanage);
} else {
System.out.println("Unknown parental status");
}
}
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
11
Signal /
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
12
Fluent Case Distinction, Example: Pattern Matching in Rust
match bob.parental_status {
Parents { mom: Some(ref mother), dad: Some(ref father) }
=> println!("Mother: {:?}, Father: {:?}", father.name, mother.name),
Orphan { orphanage: ref institute }
=> println!("Orphanage: {:?}", institute),
Unknown
=> println!("Parental status unknown"),
_ => {}
}
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
15
Fluent Case Distinction, Example
val daredevil = Person::orphan("Matt Murdock", "St Agnes Orphanage")
daredevil.parentalStatus
.caseParents [ mom, dad |
println('''Mother: «mom.name», Father: «dad.name»''')
].caseOrphan [ orphanage |
println("Orphanage: " + orphanage)
].caseUnknown [
println("Unknown parental status")
]
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
👿
16
Fluent Case Distinction, Downsides
 Complex to implement, only makes sense if used multiple times
 No flexible nested decomposition and variable binding by caller
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
17
Fluent Case Distinction, Summary / Use Cases
 Most times the powerful switch statement or multiple dispatch is good enough
 Still, this pattern can be useful for several use cases:
 Short notation for reoccurring, non trivial object decomposition
 Null-safe data access
 Can enforce exhaustive case handling or at least default case
 Alternative to inheritance hierarchies: No looking for all possible subclasses
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
19
Immutable Data Structure Patterns – Intro
 Immutable objects are easier to reason about
 No unexpected changes when passed to methods
 Can safely be shared between threads
 Interestingly better for Java GC (according to Brian Goetz)
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
20
Immutable Data Structure Patterns
 Immutable objects are tricky in some cases
 Especially demanding are:
 Object manipulation and
 Circular references
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
What??? You said immutable!
Bear with me, explanation in
3 slides
21
Immutable Data Structure Patterns: Object Instantiation
 Initialization using mutable builder objects
 Especially nice: Lambda builder pattern
 Example:
val p = ImmutablePerson.create [
firstName = "Mack"
lastName = "The Knife"
]
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
🔨
23
Immutable Data Structure Patterns: Object Manipulation
 So called “persistent data structures“
 For simple structures: Fields may have ”update” method
 Takes lambda parameter mapping old field value to new value
 Returns new immutable updated object
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
24
Immutable Object Patterns: Object Manipulation Example
class ImmutablePerson {
// ...
def ImmutablePerson firstName((String)=>String mapper) {
val newFirstName = mapper.apply(firstName)
new ImmutablePerson(newFirstName, lastName)
}
}
var p = ImmutablePerson.create […]
p = p.firstName["max"]
p = p.firstName[toFirstUpper]
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
25
Immutable Data Structure Patterns: Object Manipulation - Problem
 Cyclic references will come back to bite you on manipulation!
 Especially when automatically generating manipulators
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
26
Immutable Data Structure Patterns: Object Manipulation - Alternative
Manipulation by builder
 Create pre-filled builder from existing object
 Add some sugar for ease of use, similar to lambda builder
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
27
Immutable Data Structure Patterns: Manipulation By Builder Example
var homer = ImmutablePerson.create [
firstName = "Homer"
lastName = "Simpson"
town = "Springfield"
]
homer = homer.with [
firstName = "Max"
lastName = "Power"
]
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
29
API Xtendification – Intro
 Java APIs are not written with Xtend in mind
 Some language features of Xtend only shine when API is shaped in a certain way
 Extension methods to the rescue!
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
30
API Xtendification, Builder Extension Method; Example Before
Example calling constructor of type from JDK:
val queue = new LinkedBlockingDeque
val corePoolSize = 1
val maximumPoolSize = 5
val keepAliveTime = 200
val keepAliveTimeUnit = TimeUnit.MILLISECONDS
val pool = new ThreadPoolExecutor(maximumPoolSize, corePoolSize,
keepAliveTime, keepAliveTimeUnit, queue)
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
Whoops, wrong
parameter order
32
API Xtendification, Builder Extension Method; Example Definition
Let’s define an extension method:
static def ThreadPoolExecutor create(Class<ThreadPoolExecutor> clazz,
(ThreadPoolExecutorBuilder)=>void config) {
val builder = new ThreadPoolExecutorBuilder
config.apply(builder)
builder.build
}
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
33
API Xtendification, Builder Extension Method; Example Usage
Example using extension method
val pool = ThreadPoolExecutor.create [
corePoolSize = 1
maximumPoolSize = 5
keepAliveTime = 200
keepAliveTimeUnit = TimeUnit.MILLISECONDS
workQueue = new LinkedBlockingDeque
]
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
🔨
35
Further Xtend Patterns
10 Java Idioms Stomped with Xtend
https://www.youtube.com/watch?v=n7LUgXX_3cE
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
36
Summary
 Xtend language is pretty flexible, due to its syntax features
 Declarative looking internal DSLs are possible
 Enables new types of API patterns
 Patterns can be used to make Java APIs friendlier to use in Xtend
 Some patterns can be automated with Active Annotations
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
37
© Fraunhofer FOKUS
38
Feedback and Opinions?
 Examples repository:
https://github.com/Boereck/eclipsecon_france_2016-xtend_patterns
 Useful?
 Interesting?
 Impractical?
 Too obvious?
 What are your favorite patterns?
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
39
Image Sources
 Max Power:
http://25.media.tumblr.com/tumblr_lxxowbwXTs1qhkm9yo1_400.gif
 Joda Pug:
https://unsplash.com/photos/2Ts5HnA67k8
© Fraunhofer FOKUS
XTEND – API AND DSL DESIGN PATTERNS
41
© Fraunhofer FOKUS
Fraunhofer FOKUS
Kaiserin-Augusta-Allee 31
10589 Berlin, Germany
www.fokus.fraunhofer.de
Max Bureck
Senior Researcher
max.bureck@fokus.fraunhofer.de
Phone +49 (0)30 3463-7321
CONTACT

More Related Content

What's hot

parenscript-tutorial
parenscript-tutorialparenscript-tutorial
parenscript-tutorial
tutorialsruby
 
3150 Chapter 2 Part 1
3150 Chapter 2 Part 13150 Chapter 2 Part 1
3150 Chapter 2 Part 1
Mole Wong
 

What's hot (20)

The use of the code analysis library OpenC++: modifications, improvements, er...
The use of the code analysis library OpenC++: modifications, improvements, er...The use of the code analysis library OpenC++: modifications, improvements, er...
The use of the code analysis library OpenC++: modifications, improvements, er...
 
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Bridge TensorFlow to run on Intel nGraph backends (v0.4)Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
 
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
 
TensorFlow local Python XLA client
TensorFlow local Python XLA clientTensorFlow local Python XLA client
TensorFlow local Python XLA client
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from Python
 
Bridge TensorFlow to run on Intel nGraph backends (v0.5)
Bridge TensorFlow to run on Intel nGraph backends (v0.5)Bridge TensorFlow to run on Intel nGraph backends (v0.5)
Bridge TensorFlow to run on Intel nGraph backends (v0.5)
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
 
parenscript-tutorial
parenscript-tutorialparenscript-tutorial
parenscript-tutorial
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
C++ Core Guidelines
C++ Core GuidelinesC++ Core Guidelines
C++ Core Guidelines
 
LeFlowを調べてみました
LeFlowを調べてみましたLeFlowを調べてみました
LeFlowを調べてみました
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide show
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List Kata
 
Why rust?
Why rust?Why rust?
Why rust?
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
Gradle in a Polyglot World
Gradle in a Polyglot WorldGradle in a Polyglot World
Gradle in a Polyglot World
 
An Introduction to Plotting in Perl using PDL::Graphics::PLplot
An Introduction to Plotting in Perl using PDL::Graphics::PLplotAn Introduction to Plotting in Perl using PDL::Graphics::PLplot
An Introduction to Plotting in Perl using PDL::Graphics::PLplot
 
MeCC: Memory Comparison-based Code Clone Detector
MeCC: Memory Comparison-based Code Clone DetectorMeCC: Memory Comparison-based Code Clone Detector
MeCC: Memory Comparison-based Code Clone Detector
 
3150 Chapter 2 Part 1
3150 Chapter 2 Part 13150 Chapter 2 Part 1
3150 Chapter 2 Part 1
 
Caché acelerador de contenido
Caché acelerador de contenidoCaché acelerador de contenido
Caché acelerador de contenido
 

Viewers also liked

Types of structures
Types of structuresTypes of structures
Types of structures
carnipe
 
Architectural Professional Practice - Design
Architectural Professional Practice - DesignArchitectural Professional Practice - Design
Architectural Professional Practice - Design
Galala University
 
Building Construction
Building ConstructionBuilding Construction
Building Construction
Wilden How
 

Viewers also liked (14)

Green technologies for Paint Circulation Systems
Green technologies for Paint Circulation SystemsGreen technologies for Paint Circulation Systems
Green technologies for Paint Circulation Systems
 
Chapter 11.1: The Structure of the Solar System
Chapter 11.1: The Structure of the Solar SystemChapter 11.1: The Structure of the Solar System
Chapter 11.1: The Structure of the Solar System
 
Definition, Structure and Types of an Editorial
Definition, Structure and Types of an EditorialDefinition, Structure and Types of an Editorial
Definition, Structure and Types of an Editorial
 
1 e bil - structures - 2nd part - types of structures
1 e bil - structures - 2nd part - types of structures1 e bil - structures - 2nd part - types of structures
1 e bil - structures - 2nd part - types of structures
 
Frame Structures including sap2000
Frame Structures including sap2000Frame Structures including sap2000
Frame Structures including sap2000
 
Types of structures
Types of structuresTypes of structures
Types of structures
 
Types of structures
Types of structuresTypes of structures
Types of structures
 
Megamall: A Case Study
Megamall: A Case StudyMegamall: A Case Study
Megamall: A Case Study
 
Building construction
Building constructionBuilding construction
Building construction
 
Architectural Professional Practice - Design
Architectural Professional Practice - DesignArchitectural Professional Practice - Design
Architectural Professional Practice - Design
 
Load balancing
Load balancingLoad balancing
Load balancing
 
Building Construction
Building ConstructionBuilding Construction
Building Construction
 
Type of Loads Acting on a Structure/ Building
Type of Loads Acting on a Structure/ BuildingType of Loads Acting on a Structure/ Building
Type of Loads Acting on a Structure/ Building
 
Calculation of dead load
Calculation of dead loadCalculation of dead load
Calculation of dead load
 

Similar to Xtend api and_dsl_design_patterns_eclipse_confrance2016

Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 
20100730 phpstudy
20100730 phpstudy20100730 phpstudy
20100730 phpstudy
Yusuke Ando
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
Ajax Experience 2009
 
Scala4sling
Scala4slingScala4sling
Scala4sling
day
 

Similar to Xtend api and_dsl_design_patterns_eclipse_confrance2016 (20)

Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
20100730 phpstudy
20100730 phpstudy20100730 phpstudy
20100730 phpstudy
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Felix HTTP - Paving the road to the future
Felix HTTP - Paving the road to the futureFelix HTTP - Paving the road to the future
Felix HTTP - Paving the road to the future
 
The Cascading (big) data application framework
The Cascading (big) data application frameworkThe Cascading (big) data application framework
The Cascading (big) data application framework
 
The Cascading (big) data application framework - André Keple, Sr. Engineer, C...
The Cascading (big) data application framework - André Keple, Sr. Engineer, C...The Cascading (big) data application framework - André Keple, Sr. Engineer, C...
The Cascading (big) data application framework - André Keple, Sr. Engineer, C...
 
Infrastructure as code, using Terraform
Infrastructure as code, using TerraformInfrastructure as code, using Terraform
Infrastructure as code, using Terraform
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
Scala4sling
Scala4slingScala4sling
Scala4sling
 
FabSim: Facilitating computational research through automation on large-scale...
FabSim: Facilitating computational research through automation on large-scale...FabSim: Facilitating computational research through automation on large-scale...
FabSim: Facilitating computational research through automation on large-scale...
 
New Stuff In Php 5.3
New Stuff In Php 5.3New Stuff In Php 5.3
New Stuff In Php 5.3
 
Echtzeitapplikationen mit Elixir und GraphQL
Echtzeitapplikationen mit Elixir und GraphQLEchtzeitapplikationen mit Elixir und GraphQL
Echtzeitapplikationen mit Elixir und GraphQL
 
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
 
Angular Schematics
Angular SchematicsAngular Schematics
Angular Schematics
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Best practices tekx
Best practices tekxBest practices tekx
Best practices tekx
 
Python1
Python1Python1
Python1
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
Apache Spark 2.3 boosts advanced analytics and deep learning with Python
Apache Spark 2.3 boosts advanced analytics and deep learning with PythonApache Spark 2.3 boosts advanced analytics and deep learning with Python
Apache Spark 2.3 boosts advanced analytics and deep learning with Python
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Xtend api and_dsl_design_patterns_eclipse_confrance2016

  • 1. Max Bureck, 08. June 2016 XTEND – API AND DSL DESIGN PATTERNS
  • 2. 2 XTEND – API AND DSL DESIGN PATTERNS Intro – Xtend  Xtend is a general purpose programming language transpiling to Java source  Its syntax is flexible allowing definition of internal DSLs and interesting APIs  This presentation will show some ways how the syntax can be utilized  No detailed explanation of Xtend‘s features though © Fraunhofer FOKUS ©MatthiasHeyde/FraunhoferFOKUS©MatthiasHeyde/FraunhoferFOKUS
  • 3. 3 XTEND – API AND DSL DESIGN PATTERNS Intro – Patterns  Based on some observations from designing Xtend APIs  Some ideas inspired by other languages (e.g. Scala, F#)  Some patterns may or should be implemented via active annotations in future © Fraunhofer FOKUS ©MatthiasHeyde/FraunhoferFOKUS©MatthiasHeyde/FraunhoferFOKUS
  • 4. 4 XTEND – API AND DSL DESIGN PATTERNS Intro – The Tools Provided By Xtend  Lambdas  Call with lambda as last parameter: place after brackets; omit empty brackets strProv.apply([String s | println(s)]) ⇨ strProv.apply [println(it)]  Setter call can be written as assignment button.setText("Press Me") ⇨ button.text = "Press Me"  Extension methods emphasize("boo") ⇨ "boo".emphasize  Operator overloading operator_plus(1e15bd, 1e-4bd) ⇨ 1e15bd + 1e-4bd  Active annotations © Fraunhofer FOKUS ©MatthiasHeyde/FraunhoferFOKUS©MatthiasHeyde/FraunhoferFOKUS
  • 5. 5 XTEND – API AND DSL DESIGN PATTERNS Pattern Overview  Nested Block Syntax  Fluent Case Distinction  Immutable Data Structure Patterns  Implicit Parameter Values  Type Providers  API Xtendification © Fraunhofer FOKUS
  • 6. 6 Nested Block Syntax, Use Case  Lambda as last argument looks like a named block  Can be exploited to create internal DSLs that look like nested blocks  Declarative look, while being imperative  Especially useful when building up object trees, e.g.  UI elements  Configuration  Etc. © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 7. 7 Nested Block Syntax, Callback API Example in Java 8 server( (conf) -> { conf.setPort(80); conf.get("/hello?name=$name", (response) -> { response.header(Pair.of("content", "text/html")); return HtmlBuilder.html(response, (builder) -> { builder.h1("Hello " + builder.param("name")); }); }); }); © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 8. 8 Nested Block Syntax, Callback API Example server [ port = 80 get("/hello?name=$name") [ header("Content-Type" -> "text/html") html [ h1("Hello " + param('name')) ] ] ] © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS Assignment to setter on default argument default argument it Implicit return of last expression result Extension method Method with lambda argument Mapping operator
  • 9. 9 Nested Block Syntax, Summary  Nested block APIs reflect logical containment structures in code  Xtend reduces visual noise and enables declarative look  Can improve maintainability due to clear intent and readability of code  "Traditional" APIs may be used as nested blocks, using => operator © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 10. 10 Fluent Case Distinction, Example: Object Decomposition in Java 8 ParentalStatus parentalStatus = bob.getParentalStatus(); if(parentalStatus instanceof Parents) { Parents parents = (Parents) parentalStatus; Optional<Person> momOpt = parents.getMom(); Optional<Person> dadOpt = parents.getDad(); momOpt.ifPresent((mom) -> dadOpt.ifPresent((dad) -> { System.out.println("Mother: "+mom.getName()+", Father: "+ dad.getName()); })); } else { if(parentalStatus instanceof Orphan) { String orphanage = ((Orphan) parentalStatus).getOrphanage(); System.out.println("Orphanage: "+orphanage); } else { System.out.println("Unknown parental status"); } } © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 11. 11 Signal / © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 12. 12 Fluent Case Distinction, Example: Pattern Matching in Rust match bob.parental_status { Parents { mom: Some(ref mother), dad: Some(ref father) } => println!("Mother: {:?}, Father: {:?}", father.name, mother.name), Orphan { orphanage: ref institute } => println!("Orphanage: {:?}", institute), Unknown => println!("Parental status unknown"), _ => {} } © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 13. 15 Fluent Case Distinction, Example val daredevil = Person::orphan("Matt Murdock", "St Agnes Orphanage") daredevil.parentalStatus .caseParents [ mom, dad | println('''Mother: «mom.name», Father: «dad.name»''') ].caseOrphan [ orphanage | println("Orphanage: " + orphanage) ].caseUnknown [ println("Unknown parental status") ] © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS 👿
  • 14. 16 Fluent Case Distinction, Downsides  Complex to implement, only makes sense if used multiple times  No flexible nested decomposition and variable binding by caller © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 15. 17 Fluent Case Distinction, Summary / Use Cases  Most times the powerful switch statement or multiple dispatch is good enough  Still, this pattern can be useful for several use cases:  Short notation for reoccurring, non trivial object decomposition  Null-safe data access  Can enforce exhaustive case handling or at least default case  Alternative to inheritance hierarchies: No looking for all possible subclasses © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 16. 19 Immutable Data Structure Patterns – Intro  Immutable objects are easier to reason about  No unexpected changes when passed to methods  Can safely be shared between threads  Interestingly better for Java GC (according to Brian Goetz) © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 17. 20 Immutable Data Structure Patterns  Immutable objects are tricky in some cases  Especially demanding are:  Object manipulation and  Circular references © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS What??? You said immutable! Bear with me, explanation in 3 slides
  • 18. 21 Immutable Data Structure Patterns: Object Instantiation  Initialization using mutable builder objects  Especially nice: Lambda builder pattern  Example: val p = ImmutablePerson.create [ firstName = "Mack" lastName = "The Knife" ] © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS 🔨
  • 19. 23 Immutable Data Structure Patterns: Object Manipulation  So called “persistent data structures“  For simple structures: Fields may have ”update” method  Takes lambda parameter mapping old field value to new value  Returns new immutable updated object © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 20. 24 Immutable Object Patterns: Object Manipulation Example class ImmutablePerson { // ... def ImmutablePerson firstName((String)=>String mapper) { val newFirstName = mapper.apply(firstName) new ImmutablePerson(newFirstName, lastName) } } var p = ImmutablePerson.create […] p = p.firstName["max"] p = p.firstName[toFirstUpper] © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 21. 25 Immutable Data Structure Patterns: Object Manipulation - Problem  Cyclic references will come back to bite you on manipulation!  Especially when automatically generating manipulators © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 22. 26 Immutable Data Structure Patterns: Object Manipulation - Alternative Manipulation by builder  Create pre-filled builder from existing object  Add some sugar for ease of use, similar to lambda builder © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 23. 27 Immutable Data Structure Patterns: Manipulation By Builder Example var homer = ImmutablePerson.create [ firstName = "Homer" lastName = "Simpson" town = "Springfield" ] homer = homer.with [ firstName = "Max" lastName = "Power" ] © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 24. 29 API Xtendification – Intro  Java APIs are not written with Xtend in mind  Some language features of Xtend only shine when API is shaped in a certain way  Extension methods to the rescue! © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 25. 30 API Xtendification, Builder Extension Method; Example Before Example calling constructor of type from JDK: val queue = new LinkedBlockingDeque val corePoolSize = 1 val maximumPoolSize = 5 val keepAliveTime = 200 val keepAliveTimeUnit = TimeUnit.MILLISECONDS val pool = new ThreadPoolExecutor(maximumPoolSize, corePoolSize, keepAliveTime, keepAliveTimeUnit, queue) © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS Whoops, wrong parameter order
  • 26. 32 API Xtendification, Builder Extension Method; Example Definition Let’s define an extension method: static def ThreadPoolExecutor create(Class<ThreadPoolExecutor> clazz, (ThreadPoolExecutorBuilder)=>void config) { val builder = new ThreadPoolExecutorBuilder config.apply(builder) builder.build } © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 27. 33 API Xtendification, Builder Extension Method; Example Usage Example using extension method val pool = ThreadPoolExecutor.create [ corePoolSize = 1 maximumPoolSize = 5 keepAliveTime = 200 keepAliveTimeUnit = TimeUnit.MILLISECONDS workQueue = new LinkedBlockingDeque ] © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS 🔨
  • 28. 35 Further Xtend Patterns 10 Java Idioms Stomped with Xtend https://www.youtube.com/watch?v=n7LUgXX_3cE © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 29. 36 Summary  Xtend language is pretty flexible, due to its syntax features  Declarative looking internal DSLs are possible  Enables new types of API patterns  Patterns can be used to make Java APIs friendlier to use in Xtend  Some patterns can be automated with Active Annotations © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 31. 38 Feedback and Opinions?  Examples repository: https://github.com/Boereck/eclipsecon_france_2016-xtend_patterns  Useful?  Interesting?  Impractical?  Too obvious?  What are your favorite patterns? © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 32. 39 Image Sources  Max Power: http://25.media.tumblr.com/tumblr_lxxowbwXTs1qhkm9yo1_400.gif  Joda Pug: https://unsplash.com/photos/2Ts5HnA67k8 © Fraunhofer FOKUS XTEND – API AND DSL DESIGN PATTERNS
  • 33. 41 © Fraunhofer FOKUS Fraunhofer FOKUS Kaiserin-Augusta-Allee 31 10589 Berlin, Germany www.fokus.fraunhofer.de Max Bureck Senior Researcher max.bureck@fokus.fraunhofer.de Phone +49 (0)30 3463-7321 CONTACT

Editor's Notes

  1. If not xperienced: hopefully learn on the go Example ideas inspired by pop culture
  2. Library to xtend some Java 8 types
  3. Lambda: if only one argument: implicit argument it
  4. Leaving out how imparative APIs can be used in block style
  5. Silly example (stringly typed) designed to show off Xtend features See active annotation version talk referenced later
  6. „It“ can be used same as „this“ (as in leaving out) Exact same API !!
  7. Xtend: Switch only on types; Generic library solution not possible Datatype specific APIs
  8. Checks which fields are null Just hiding boilerplate Direct apporoach can be optimized to never allocate: Initially returning immutable object for state „not found“ Always returning same obj when match not found yet Returning a simple NoOp singleton after match found
  9. IMG src: https://unsplash.com/photos/2Ts5HnA67k8 http://creativecommons.org/publicdomain/zero/1.0/
  10. Exhaustiveness could also be checked at runtime, if no default case enforced Short notation could even be implemented via extension methods Direct version: exhaustion works best when cases return value
  11. Working with them can come with memory and/or runtime overhead Use where appropriate
  12. Not a person who won‘t stop talking Looks like constuctor call with named params Syntactic sugar for builder use Drawbacks: Assignment only if setter does not returns void. Incompatible with fluent builder pattern. Compiler cannot ensure all mandatory fields are set What is the problem with cyclic references?
  13. Alternative „with“ method, simply pass in value: Have a look at Java 8 DateTime classes. Why lambda? Next slide! Property access is variation of “over” combinator of “lense” pattern in Haskell.
  14. See creating new ImmutablePerson, how cyclic reference? We need info how to update field holding cyclic ref Apart from cycles: shallow copies can be used if all objects are immutable.
  15. Only flat copy, if only immutable types (again: cyclic references are a problem) Imparative code style possible in block IMG src: http://25.media.tumblr.com/tumblr_lxxowbwXTs1qhkm9yo1_400.gif
  16. Think about a containment hierarchy Root object: appearently Domain Driven Design practice Nesting alternative: monatic data types
  17. Context first, lambda last
  18. Builder is the answer
  19. Basically named parameters Looks like static method call: Lambda builder pattern Can have default values for each param