Hi to all, I started using Kotlin little over two...
# getting-started
g
Hi to all, I started using Kotlin little over two month to write a library, since then I started listening to Talking Kotlin and I started to hear the concept of DSL. The more I hear DSL in a conversation, seems to me that what it means is a specific library that is going to be used within a specific language. I would like for someone to explain what are the cases where to can call some code base a DSL.
r
DSL is short for "Domain Specific Language". So, basically, the opposite of a general purpose language (which means most programming languages (GPLs) like Java, Kotlin, C, etc.). HTML and CSS could be considered DSLs, as they are targeted at the specific domains of describing and styling Hypertext Documents. UML is another example of a visual DSL for the specific Domain of describing software architecture/behavior. So you could argue that any domain-specific library might be considered a DSL, but usually, it's apparent that you're still actually dealing with the underlying GPL. When we say DSL in the context of Kotlin, we're referring to an API that's built in a specific way meant to make the GPL (Kotlin) fade into the background, making it seem like you're actually programming in a DSL. "Lambdas with Receivers" is the core feature in Kotlin that makes this possible.
💡 1
g
So DSL is used loosely to mean API library.
🚫 1
Interesting
Thanks
r
Well, a library that exposes a specific kind of API, but yes.
And technically, DSL doesn't refer to the whole library, just the part of its API that can be used like a DSL. So, ktor for example is not a DSL itself, but it exposes a DSL to configure your webserver. The exceptions are libraries whose whole point it is to offer a DSL over something -
kotlinx.html
for example is a DSL to produce HTML code.
j
Technically what Kotlin supports is so-called internal domain specific languages. It's internal because it can be done through syntax features that Kotlin has like for example being able to move a lambda block argument outside the parameters of a function call and omit parentheses if that was the only parameter. Basically it allows you do little languages that drive a framework without having to bother with a parser or a grammar. So, foo({param ->...}) is equivalent to foo {param-> ...}. There are a few more features but that would be the most important one. So if you design a library, you can offer a simple DSL style way of using it to users. Check out my Kotlin library for Elasticsearch for some examples: https://github.com/jillesvangurp/es-kotlin-wrapper-client
👍 2
l
a DSL is anything that provides a simple API to create or configure something "in terms of the specific Domain". so HTML is technically a DSL: it allows you to describe the layout of a website "in terms of layout-words". the
kotlinx.html
library is an implementation of a HTML-like DSL for Kotlin. another DSL based library is
ktor
, the web server / client. DSL is also a great way of doing complex configuration.
a
The easiest way to get started with this is thinking of a need where you have to set some complex configuration that the builder pattern might be able to accomplish. A good example is any Gradle config file, the domain is a "JVM project"