SlideShare a Scribd company logo
Roll your own
DSL with Kotlin
A lightning talk by Miquel Beltran

Android Developer @ Nebenan.de
@Miqubel
What’s a DSL?
@Miqubel
android {
  compileSdkVersion 26
  buildToolsVersion "26.0.2"
  defaultConfig {
…
@Miqubel
SELECT name FROM users WHERE age >= 30
@Miqubel
verticalLayout {
padding = dip(32)
imageView(…).lparams {
margin = dip(16)
gravity = Gravity.CENTER
}
…
@Miqubel
// Old School Java Style
val ratingView = RatingView()
ratingView.setStars(3)
ratingView.setMaxStars(5)
// Kotlin DSL Style
rating {
initial = 3
maximum = 5
}
@Miqubel
// Creating our DSL
fun rating(func:() -> Unit)
// Using our DSL
rating({})
@Miqubel
// Creating our DSL
fun rating(func:() -> Unit)
// Using our DSL
rating({
})
@Miqubel
// Creating our DSL
fun rating(func:() -> Unit)
// Using our DSL
rating {
}
@Miqubel
// Creating our DSL
fun rating(func:() -> Unit)
// Using our DSL
rating {
initial = 3
maximum = 5
}
@Miqubel
// Creating our DSL
fun rating(func: RatingDsl.() -> Unit)
// Using our DSL
rating {
initial = 3
maximum = 5
}
@Miqubel
// Creating our DSL
fun rating(func: RatingDsl.() -> Unit)
class RatingDsl(var initial: Int = 1,
var maximum: Int = 5)
// Using our DSL
rating {
initial = 3
maximum = 5
}
@Miqubel
// Creating our DSL
fun rating(func: RatingDsl.() -> Unit)
= RatingDsl().apply { func() }
class RatingDsl(var initial: Int = 1,
var maximum: Int = 5)
// Using our DSL
rating {
initial = 3
maximum = 5
}
@Miqubel
// Creating our DSL
fun rating(func: RatingDsl.() -> Unit)
= RatingDsl()
.apply { func() }
.build()
class RatingDsl(var initial: Int = 1,
var maximum: Int = 5) {
fun build(): RatingView {
return RatingView().apply {
setStars(initial)
setMaxStars(maximum)
}
}
}
// Using our DSL
rating {
initial = 3
maximum = 5
}
@Miqubel
Thanks!
If you would be interested in a full length
talk about this, let me know!
@Miqubel

More Related Content

Similar to Roll your own DSL with Kotlin

Similar to Roll your own DSL with Kotlin (20)

Scilab Modelica conference 20150921
Scilab Modelica conference 20150921Scilab Modelica conference 20150921
Scilab Modelica conference 20150921
 
Artists Only
Artists OnlyArtists Only
Artists Only
 
Why I ❤️ Kotlin Multiplatform (and want YOU to also ❤️ Kotlin Multiplatform)
Why I ❤️ Kotlin Multiplatform (and want YOU to also ❤️ Kotlin Multiplatform)Why I ❤️ Kotlin Multiplatform (and want YOU to also ❤️ Kotlin Multiplatform)
Why I ❤️ Kotlin Multiplatform (and want YOU to also ❤️ Kotlin Multiplatform)
 
Coding Defensively Against APIs iOSoHo 2015.03 March - Michael Sanderson
Coding Defensively Against APIs iOSoHo 2015.03 March - Michael SandersonCoding Defensively Against APIs iOSoHo 2015.03 March - Michael Sanderson
Coding Defensively Against APIs iOSoHo 2015.03 March - Michael Sanderson
 
Tungsten University: Replicate Between MySQL And Oracle
Tungsten University: Replicate Between MySQL And OracleTungsten University: Replicate Between MySQL And Oracle
Tungsten University: Replicate Between MySQL And Oracle
 
Satellite ground station
Satellite ground stationSatellite ground station
Satellite ground station
 
JavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and FutureJavaScript Modules Past, Present and Future
JavaScript Modules Past, Present and Future
 
GumGum: Multi-Region Cassandra in AWS
GumGum: Multi-Region Cassandra in AWSGumGum: Multi-Region Cassandra in AWS
GumGum: Multi-Region Cassandra in AWS
 
UDP Report
UDP ReportUDP Report
UDP Report
 
chapter-1.pptx
chapter-1.pptxchapter-1.pptx
chapter-1.pptx
 
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQLMongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
 
Say bye to Fragments with Conductor & Kotlin
Say bye to Fragments with Conductor & KotlinSay bye to Fragments with Conductor & Kotlin
Say bye to Fragments with Conductor & Kotlin
 
Idiomatic Gradle Plugin Writing - GradleSummit 2016
Idiomatic Gradle Plugin Writing - GradleSummit 2016Idiomatic Gradle Plugin Writing - GradleSummit 2016
Idiomatic Gradle Plugin Writing - GradleSummit 2016
 
Cheat-sheet_Koin_2023.pdf
Cheat-sheet_Koin_2023.pdfCheat-sheet_Koin_2023.pdf
Cheat-sheet_Koin_2023.pdf
 
Cisco 300-101 Exam Questions Answers
Cisco 300-101 Exam Questions AnswersCisco 300-101 Exam Questions Answers
Cisco 300-101 Exam Questions Answers
 
21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx
 
Compose in Theory
Compose in TheoryCompose in Theory
Compose in Theory
 
OpenDaylight and YANG
OpenDaylight and YANGOpenDaylight and YANG
OpenDaylight and YANG
 
Verified CKAD Exam Questions and Answers
Verified CKAD Exam Questions and AnswersVerified CKAD Exam Questions and Answers
Verified CKAD Exam Questions and Answers
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 

Roll your own DSL with Kotlin

  • 1. Roll your own DSL with Kotlin A lightning talk by Miquel Beltran Android Developer @ Nebenan.de @Miqubel
  • 3. android {   compileSdkVersion 26   buildToolsVersion "26.0.2"   defaultConfig { … @Miqubel
  • 4. SELECT name FROM users WHERE age >= 30 @Miqubel
  • 5. verticalLayout { padding = dip(32) imageView(…).lparams { margin = dip(16) gravity = Gravity.CENTER } … @Miqubel
  • 6. // Old School Java Style val ratingView = RatingView() ratingView.setStars(3) ratingView.setMaxStars(5) // Kotlin DSL Style rating { initial = 3 maximum = 5 } @Miqubel
  • 7. // Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating({}) @Miqubel
  • 8. // Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating({ }) @Miqubel
  • 9. // Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating { } @Miqubel
  • 10. // Creating our DSL fun rating(func:() -> Unit) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel
  • 11. // Creating our DSL fun rating(func: RatingDsl.() -> Unit) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel
  • 12. // Creating our DSL fun rating(func: RatingDsl.() -> Unit) class RatingDsl(var initial: Int = 1, var maximum: Int = 5) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel
  • 13. // Creating our DSL fun rating(func: RatingDsl.() -> Unit) = RatingDsl().apply { func() } class RatingDsl(var initial: Int = 1, var maximum: Int = 5) // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel
  • 14. // Creating our DSL fun rating(func: RatingDsl.() -> Unit) = RatingDsl() .apply { func() } .build() class RatingDsl(var initial: Int = 1, var maximum: Int = 5) { fun build(): RatingView { return RatingView().apply { setStars(initial) setMaxStars(maximum) } } } // Using our DSL rating { initial = 3 maximum = 5 } @Miqubel
  • 15. Thanks! If you would be interested in a full length talk about this, let me know! @Miqubel