
withOnDrawerItemClickListener call, let's implement just that. withDrawerLayout(R.layout.material_drawer_fits_not) Here's the code, using the builders in the original API: DrawerBuilder() Let's take a look at the drawer we're going to be creating in our example. This is what my library, MaterialDrawerKt provides a convenient Kotlin DSL wrapper for.

The original library is the wonderful and hugely popular MaterialDrawer by Mike Penz, which allows you to create complex, good looking, and customized navigation drawers in your application, all via various Builder objects in Java, with no XML writing involved on your part. It just so happens that I'm the creator and maintainer of a library that does this very thing, and a small part of its implementation is what we're going to be using as our example. All you have to worry about is designing the API you provide with your wrapper, since all the implementation for whatever your DSL provides its users is already done inside the builder! Our example case For example, instances of a Kotlin class can be seamlessly created and operated in Java methods.but there is many differences require attention. Wrapping a builder like this is a good exercise in just pure DSL design. Kotlin code can be easily called from Java.

No doubt you've come across the builder pattern in Java before, for example if you're an Android developer, you must've used an AlertDialog.Builder, an OkHttpClient.Builder, or a Retrofit.Builder at some point. Today I'd like to show you how to implement a certain kind of DSL - we're going to be wrapping an existing Java Builder in Kotlin.

They allow us to flex some of the most exciting language features while accomplishing more readable and maintainable solutions in our code. By Márton Braun 2 Comments From Java Builders to Kotlin DSLs IntroductionĭSLs - Domain Specific Languages - are an ever trending topic in Kotlin circles.
