SlideShare a Scribd company logo
1 of 11
Kotlin for Android
May 24, 2018
y/kotlin-workbook
Building DSLs
y/kotlin-workbook
MockResponse mockResponse = new MockResponseBuilder()
.withCode(200)
.withHeader("Content-Type", "application/json")
.withBody(new Buffer()...)
.build();
Why DSLs?
DSLs
y/kotlin-workbook
MockResponse mockResponse = new MockResponseBuilder()
.withCode(200)
.withHeader("Content-Type", "application/json")
.withBody(new Buffer()...)
.build();
Why DSLs?
DSLs
y/kotlin-workbook
MockResponse mockResponse = new MockResponseBuilder()
.withCode(200)
.withHeader("Content-Type", "application/json")
.withBody(new Buffer()...)
.build();
public class MockResponseBuilder {
public MockResponseBuilder withCode(int code) {}
public MockResponseBuilder withHeader(String name,
String value) {}
public MockResponseBuilder withBody(Buffer body) {}
public MockResponse build() {}
}
Why DSLs?
DSLs
y/kotlin-workbook
val mockResponse = mockResponse {
code(200)
header("Content-Type", "application/json")
body {
readFromTestAsset("success.json")
}
}
MockResponse mockResponse = new MockResponseBuilder()
.withCode(200)
.withHeader("Content-Type", "application/json")
.withBody(new Buffer()...)
.build();
public class MockResponseBuilder {
public MockResponseBuilder withCode(int code) {}
public MockResponseBuilder withHeader(String name,
String value) {}
public MockResponseBuilder withBody(Buffer body) {}
public MockResponse build() {}
}
Why DSLs?
DSLs
y/kotlin-workbook
val mockResponse = mockResponse {
code(200)
header("Content-Type", "application/json")
body {
readFromTestAsset("success.json")
}
}
fun mockResponse(
init: MockResponseBuilder.() -> Unit
): MockResponse {
return MockResponseBuilder().apply(init).build()
}
public class MockResponseBuilder {
public MockResponseBuilder withCode(int code) {}
public MockResponseBuilder withHeader(String name,
String value) {}
public MockResponseBuilder withBody(Buffer body) {}
public MockResponse build() {}
}
DSL How To
DSLs
y/kotlin-workbook
val mockResponse = mockResponse {
code(200)
header("Content-Type", "application/json")
body {
readFromTestAsset("success.json")
}
}
fun mockResponse(
init: MockResponseBuilder.() -> Unit
): MockResponse {
return MockResponseBuilder().apply(init).build()
}
class MockResponseBuilder internal constructor() {
fun code(code: Int)
fun header(name: String, value: String)
fun body(init: Buffer.() -> Unit)
internal fun build(): MockResponse
}
DSL How To
DSLs
y/kotlin-workbook
val mockResponse = mockResponse {
code(200)
header("Content-Type", "application/json")
body {
readFromTestAsset("success.json")
}
}
fun mockResponse(
init: MockResponseBuilder.() -> Unit
): MockResponse {
return MockResponseBuilder().apply(init).build()
}
fun Buffer.readFromTestAsset(responseJsonPath: String)
class MockResponseBuilder internal constructor() {
fun code(code: Int)
fun header(name: String, value: String)
fun body(init: Buffer.() -> Unit)
internal fun build(): MockResponse
}
DSL How To
DSLs
y/kotlin-workbook
val mockResponse = mockResponse {
code(200)
header("Content-Type", "application/json")
body {
readFromTestAsset("success.json")
}
}
fun mockResponse(
init: MockResponseBuilder.() -> Unit
): MockResponse {
return MockResponseBuilder().apply(init).build()
}
fun Buffer.readFromTestAsset(responseJsonPath: String)
@DslMarker annotation class MockResponseDsl
@MockResponseDsl
class MockResponseBuilder internal constructor() {
fun code(code: Int)
fun header(name: String, value: String)
fun body(init: Buffer.() -> Unit)
internal fun build(): MockResponse
}
DSL How To
DSLs
y/kotlin-workbook
Questions?
Thanks for attending!

More Related Content

Similar to Kotlin For Android - How to Build DSLs (part 7 of 7)

Similar to Kotlin For Android - How to Build DSLs (part 7 of 7) (20)

A New Chapter of Data Processing with CDK
A New Chapter of Data Processing with CDKA New Chapter of Data Processing with CDK
A New Chapter of Data Processing with CDK
 
Morphia, Spring Data & Co.
Morphia, Spring Data & Co.Morphia, Spring Data & Co.
Morphia, Spring Data & Co.
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Laravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swagger
 
Alloy Tips & Tricks #TiLon
Alloy Tips & Tricks #TiLonAlloy Tips & Tricks #TiLon
Alloy Tips & Tricks #TiLon
 
Modern Android Development - Epoxy
Modern Android Development - EpoxyModern Android Development - Epoxy
Modern Android Development - Epoxy
 
Spring Data MongoDB 介紹
Spring Data MongoDB 介紹Spring Data MongoDB 介紹
Spring Data MongoDB 介紹
 
New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)
 
Refactoring
RefactoringRefactoring
Refactoring
 
React - render() to DOM - Boris Dinkevich - Codemotion Milan 2016
React - render() to DOM - Boris Dinkevich - Codemotion Milan 2016React - render() to DOM - Boris Dinkevich - Codemotion Milan 2016
React - render() to DOM - Boris Dinkevich - Codemotion Milan 2016
 
MongoDB.local Berlin: MongoDB Mobile
MongoDB.local Berlin: MongoDB MobileMongoDB.local Berlin: MongoDB Mobile
MongoDB.local Berlin: MongoDB Mobile
 
MobiConf 2018 | Room: an SQLite object mapping library
MobiConf 2018 | Room: an SQLite object mapping library MobiConf 2018 | Room: an SQLite object mapping library
MobiConf 2018 | Room: an SQLite object mapping library
 
Spring Data, Jongo & Co.
Spring Data, Jongo & Co.Spring Data, Jongo & Co.
Spring Data, Jongo & Co.
 
Dependency Injections in Kotlin
Dependency Injections in KotlinDependency Injections in Kotlin
Dependency Injections in Kotlin
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015
 
What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?
 
Refactoring
RefactoringRefactoring
Refactoring
 
Getting started with building your own standalone Gradle plugin
Getting started with building your own standalone Gradle pluginGetting started with building your own standalone Gradle plugin
Getting started with building your own standalone Gradle plugin
 
Metaprogramming in .NET
Metaprogramming in .NETMetaprogramming in .NET
Metaprogramming in .NET
 
Dependency injectionpreso
Dependency injectionpresoDependency injectionpreso
Dependency injectionpreso
 

More from Gesh Markov

More from Gesh Markov (6)

Kotlin For Android - Collections APIs (part 6 of 7)
Kotlin For Android - Collections APIs (part 6 of 7)Kotlin For Android - Collections APIs (part 6 of 7)
Kotlin For Android - Collections APIs (part 6 of 7)
 
Kotlin For Android - Useful Kotlin Standard Functions (part 5 of 7)
Kotlin For Android - Useful Kotlin Standard Functions (part 5 of 7)Kotlin For Android - Useful Kotlin Standard Functions (part 5 of 7)
Kotlin For Android - Useful Kotlin Standard Functions (part 5 of 7)
 
Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
Kotlin For Android - Constructors and Control Flow (part 2 of 7)Kotlin For Android - Constructors and Control Flow (part 2 of 7)
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
 
Kotlin For Android - Basics (part 1 of 7)
Kotlin For Android - Basics (part 1 of 7)Kotlin For Android - Basics (part 1 of 7)
Kotlin For Android - Basics (part 1 of 7)
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

Kotlin For Android - How to Build DSLs (part 7 of 7)

  • 1. Kotlin for Android May 24, 2018 y/kotlin-workbook
  • 3. MockResponse mockResponse = new MockResponseBuilder() .withCode(200) .withHeader("Content-Type", "application/json") .withBody(new Buffer()...) .build(); Why DSLs? DSLs y/kotlin-workbook
  • 4. MockResponse mockResponse = new MockResponseBuilder() .withCode(200) .withHeader("Content-Type", "application/json") .withBody(new Buffer()...) .build(); Why DSLs? DSLs y/kotlin-workbook
  • 5. MockResponse mockResponse = new MockResponseBuilder() .withCode(200) .withHeader("Content-Type", "application/json") .withBody(new Buffer()...) .build(); public class MockResponseBuilder { public MockResponseBuilder withCode(int code) {} public MockResponseBuilder withHeader(String name, String value) {} public MockResponseBuilder withBody(Buffer body) {} public MockResponse build() {} } Why DSLs? DSLs y/kotlin-workbook
  • 6. val mockResponse = mockResponse { code(200) header("Content-Type", "application/json") body { readFromTestAsset("success.json") } } MockResponse mockResponse = new MockResponseBuilder() .withCode(200) .withHeader("Content-Type", "application/json") .withBody(new Buffer()...) .build(); public class MockResponseBuilder { public MockResponseBuilder withCode(int code) {} public MockResponseBuilder withHeader(String name, String value) {} public MockResponseBuilder withBody(Buffer body) {} public MockResponse build() {} } Why DSLs? DSLs y/kotlin-workbook
  • 7. val mockResponse = mockResponse { code(200) header("Content-Type", "application/json") body { readFromTestAsset("success.json") } } fun mockResponse( init: MockResponseBuilder.() -> Unit ): MockResponse { return MockResponseBuilder().apply(init).build() } public class MockResponseBuilder { public MockResponseBuilder withCode(int code) {} public MockResponseBuilder withHeader(String name, String value) {} public MockResponseBuilder withBody(Buffer body) {} public MockResponse build() {} } DSL How To DSLs y/kotlin-workbook
  • 8. val mockResponse = mockResponse { code(200) header("Content-Type", "application/json") body { readFromTestAsset("success.json") } } fun mockResponse( init: MockResponseBuilder.() -> Unit ): MockResponse { return MockResponseBuilder().apply(init).build() } class MockResponseBuilder internal constructor() { fun code(code: Int) fun header(name: String, value: String) fun body(init: Buffer.() -> Unit) internal fun build(): MockResponse } DSL How To DSLs y/kotlin-workbook
  • 9. val mockResponse = mockResponse { code(200) header("Content-Type", "application/json") body { readFromTestAsset("success.json") } } fun mockResponse( init: MockResponseBuilder.() -> Unit ): MockResponse { return MockResponseBuilder().apply(init).build() } fun Buffer.readFromTestAsset(responseJsonPath: String) class MockResponseBuilder internal constructor() { fun code(code: Int) fun header(name: String, value: String) fun body(init: Buffer.() -> Unit) internal fun build(): MockResponse } DSL How To DSLs y/kotlin-workbook
  • 10. val mockResponse = mockResponse { code(200) header("Content-Type", "application/json") body { readFromTestAsset("success.json") } } fun mockResponse( init: MockResponseBuilder.() -> Unit ): MockResponse { return MockResponseBuilder().apply(init).build() } fun Buffer.readFromTestAsset(responseJsonPath: String) @DslMarker annotation class MockResponseDsl @MockResponseDsl class MockResponseBuilder internal constructor() { fun code(code: Int) fun header(name: String, value: String) fun body(init: Buffer.() -> Unit) internal fun build(): MockResponse } DSL How To DSLs y/kotlin-workbook

Editor's Notes

  1. Most commonly custom DSLs are used to build objects or hierarchy of objects in a constrained domain
  2. The main problem with Java builders is that nested builders have to be initialized in advance, not in-line This makes custom DSLs much more expressive
  3. Last problem is that we still need to create the Buffer in advance
  4. ALMOST END OF SLIDES - NEXT SLIDE IS LAST - Q&A STARTS