Android make Kotlin DSL (Part1)

Updated 29 November 2019

Save

A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains. Some examples of using DSLs (SQL, RegExp, Gradle).

What need we know to start DSL?

1.Lambda expressions
2.Higher-Order Function
3.Lambdas With Receivers

1.Lamda expression :
Lambda expression is a short way to define a function. A function without a name is called the anonymous function. Lambdas Expressions are anonymous functions that we can pass them as arguments to methods or return.

Lambda Expressions look like below:

2.Higher-Order Function

A higher-order function is a function that can take other functions as parameters and returns other functions.

Now Let’s start by converting a data model class into DSL

Now let’s make the DSL higher-order function:

Finally, we have created our data model object in the DSL way.

It’s worked fine, but you need to use it to access the properties instead of accessing them directly. We can fix it with the help of lambdas with receivers.

We will come back soon with part 2 lambdas with receivers

I hope this blog is useful to you. Thanks for reading.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home