[MUSIC PLAYING]
JEREMY WALKER: All right.
Hi, my name's Jeremy Walker.
I'm a developer platform engineer here at Google.
And I wanted to talk to you a little bit about how
I use Kotlin to make watch face creation much easier
on WEAR OS.
So, watch faces are kind of hard right now.
And you have to write about 600 plus lines of code.
It's not like a nice app you do in Android or WEAR OS,
where you can declare a lot of the UI in an XML format.
You actually have to manually paint everything.
And then you have to include a bunch of code just
to make sure when your in ambient mode
you're not hurting the battery life.
So, I was trying to think of ways
to improve this and last year at I/O we announced
official support for Kotlin.
So I started thinking, how could I
use Kotlin to make this easier?
And I did convert the whole thing
to Kotlin, which did reduce the code lines a lot.
But I found something else with Kotlin
that made it even more cool.
And that was something called DSL.
So, what is Kotlin DSL?
The best way to understand what a domain specific language is,
is to compare it to a regular programming language.
So, again, domain specific language.
That, for me, didn't really help.
But, if you compare it to, like, a general programming
language like Kotlin or Java, where
you have a bunch of keywords, you have classes, methods,
and you make a nice big app, the opposite, on the other side,
is the DSL, which focuses on a specific task or domain.
So, it forgoes a lot of functionality
and lets you just do that specific task.
You've actually probably used external DSL and not even
realized it.
For example, SQL.
For manipulating databases.
That's a DSL.
So, it's regular expressions for manipulating strings.
So, they each have an independent syntax.
They reduce a lot of functionality.
You don't need, like, methods or a class to make them work.
You're not going to write a full on application in them either.
At least, I hope not.
For SQL, the first keyword kind of
indicates, as a verb, what you're going to do.
Maybe you're going to Select or you're going to Update.
The disadvantage here, as you probably know,
is you have to put that as a string literal.
So, you have to pray and hope that you spelled everything
right until runtime and then maybe it fails
and you have to hack through it and figure out what's going on.
So, with Kotlin DSL it actually extracts that out of the string
and it puts it right in your code.
You can have type checking, you can
have code hints, and all that good stuff
that comes with your IDE.
So, now that you kind of have an idea of what it is,
I'd like to show you two structures really quick.
And I use the latter, but there's
one called "chained method calls,"
and the other one is "nesting of lambdas."
So, recognizing DSL is really subjective.
You know it when you see it.
So, let's see some.
So, this is Kotlin DSL for SQL.
You could probably look at it, and if you know SQL,
you can understand it right away.
And it's all verbs like Slice, and then I Select All,
and then I Group By, then I Order By, and then I Limit.
And this is all type checked and you get code hinting,
so you don't spell anything wrong.
And this is pretty great and it's understandable.
This is DSL.
But the DSL for the watch face, which I really liked,
was a "nested lambda" structure.
So, this is to create a watch face.
You can see right away, Create A Watch Face is the verb.
But if you look at my structure, you don't know anything--
you may not know anything about making a watch face.
But you can probably understand what's going on right away.
Analog watch face.
OK, it's not digital so it has arms.
Colors.
OK, I can figure that out.
The dimensions for the hand, the hour hand, the minute hand,
the second hand.
OK, I understand that.
Watch face background image.
So, that's all.
It's very declarative.
So you can kind of understand right away.
And this is all type checked and I get code hints
and all that good stuff.
And in the end I get this nice little watch face with no work.
Now the 600 lines, they didn't all go away.
I put them in a helper class and combined it
with the more important class.
It interprets this DSL.
But if you're making a watch face
you only need to know about the DSL.
So, what's next?
This is kind of an experiment I did as a code lab.
So check out the code lab.
It'll only take you 15 minutes to make a new watch face.
Just search for Kotlin watch face code
lab-- jeez, that's really long.
Just search for Google code lab.
And it's under the WEAR OS, or search under Kotlin.
You can see how to make a watch face.
And you can see the code--
the source code for how I made the transition between DSL
and interpreted it into a nice little watch face.
But more importantly, hopefully now you're
a little bit interested in DSL and say,
hey, I could use this in one of my projects
to make it a little bit better.
Or you can go out and use something like the DSL for SQL.
There's one for testing.
And one that's more similar to mine.
You can even do HDML in your Kotlin coding, type checked.
Which is kind of what I modeled mine after.
So, thank you for the talk--
letting me talk.
And hopefully I've kind of got you
a little bit interested in DSL.
Thank you.
[APPLAUSE]
[MUSIC PLAYING]