Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java One - Designing a DSL in Kotlin

790 views

Published on

How to design a Domain-Specific Language using features offered by the Kotlin language, using the Vaadin API as a example.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Java One - Designing a DSL in Kotlin

  1. 1. DESIGNING A DSL IN KOTLIN @NICOLAS_FRANKEL
  2. 2. ME, MYSELF AND I 2  By day • Consultant  By night • Developer • Blogger • Book author • Teacher/trainer @nicolas_frankel #kotlin #dsl #kaadin
  3. 3. HYBRIS, AN SAP COMPANY 3 @nicolas_frankel #kotlin #dsl #kaadin
  4. 4. A DSL? 4 “A Domain-Specific Language is a computer language specialized to a particular application domain. This is in contrast to a General-Purpose Language, which is broadly applicable across domains.” -- Wikipedia @nicolas_frankel #kotlin #dsl #kaadin
  5. 5. GPL/DSL EXAMPLES GPL DSL XML • HTML • MathML Java • Hamcrest • AssertJ • Fest Assert Kotlin • Anko • Kaadin • Bean DSL 5 @nicolas_frankel #kotlin #dsl #kaadin
  6. 6. DESIGNING A DSL IN JAVA 6 Method Sequencing Method Chaining Nested Method Calls Lambdas https://dzone.com/articles/building-a-dsl-in-java @nicolas_frankel #kotlin #dsl #kaadin
  7. 7. ASSERTJ SAMPLE assertThat(frodo.getName()) .startsWith("Fro") .endsWith("do") .isEqualToIgnoringCase("frodo"); assertThat(fellowshipOfTheRing) .filteredOn(c -> c.getName().contains("o")) .containsOnly(aragorn, frodo, legolas) .extracting(c -> c.getRace().getName()) .contains("Hobbit", "Elf", "Man"); 7 @nicolas_frankel #kotlin #dsl #kaadin
  8. 8. KOTLIN 8 “Statically typed programming language for the JVM, Android and the browser” -- http://kotlinlang.org/ @nicolas_frankel #kotlin #dsl #kaadin
  9. 9. A FEW FACTS 9 Developed by JetBrains • Soon co-managed by Google Open Source Compiles to • JVM bytecode • JavaScript • (Native) A "simpler Scala" @nicolas_frankel #kotlin #dsl #kaadin
  10. 10. KOTLIN FEATURES 10 Functional and object-oriented Statically-typed Null safe No checked exceptions Named & optional arguments Lambdas Extension functions Java compatibility (And more...) @nicolas_frankel #kotlin #dsl #kaadin
  11. 11. SAMPLE: VAADIN UI public class SampleUI extends UI { @Override protected void init(VaadinRequest req) { setTheme("valo"); VerticalLayout layout = new VerticalLayout(); Label label = new Label("<strong>Hello</strong>", HTML); TextField field = new TextField("world!"); layout.addComponents(label, field); layout.setMargin(true); setContent(layout); } } 11 @nicolas_frankel #kotlin #dsl #kaadin
  12. 12. OUR GOAL FOR DSL 12 “Looks” declarative... But is code @nicolas_frankel #kotlin #dsl #kaadin
  13. 13. DECLARATIVE VAADIN UI class KaadinSampler : UI() { override fun init(req: VaadinRequest) { theme = "valo" verticalLayout(margin = true) { html("<strong>Hello</strong>") textField("world!") } } } 13 @nicolas_frankel #kotlin #dsl #kaadin
  14. 14. DEMO! 14 @nicolas_frankel #kotlin #dsl #kaadin
  15. 15. WHY NOT GROOVY? email { from 'dsl-guru@mycompany.com’ to 'john.doe@waitaminute.com’ subject 'The pope has resigned!’ body { p 'Really, the pope has resigned!’ } } 15 @nicolas_frankel #kotlin #dsl #kaadin
  16. 16. WHY NOT SCALA? object SquareRoot extends Baysick { def main(args:Array[String]) = { 10 PRINT "Enter a number" 20 INPUT 'n 30 PRINT "Square root of " % "'n is " % SQRT('n) 40 END RUN } } 16 @nicolas_frankel #kotlin #dsl #kaadin
  17. 17. TAKEAWAYS 1. 100% Java interoperability 2. Properties 3. Extension Functions 4. Named parameters 5. Default values 6. Lambdas with receiver 7. (Operator overloading) 8. (Infix) 17 @nicolas_frankel #kotlin #dsl #kaadin
  18. 18. Q&A 18 http://blog.frankel.ch/ @nicolas_frankel http://frankel.in/ https://git.io/vd8d9 https://git.io/vHGER @nicolas_frankel #kotlin #dsl #kaadin

×