Kotlin Uses Dsl to Build Syntactic Structure

Kotlin uses DSL to build syntactic structure Solution


Kotlin uses DSL to build grammatical structures and it is enough to read this article~

Foreword
DSL is not provided for the Kotlin language alone. You may not know what DSL is, but I dare to say that as long as you are an Android developer, you must have used and have been using DSL. So what is DSL? What kind of functions can be achieved using DSL?

build syntactic structure .what is DSL


The whole process of DSL is Domain Specific Language, that is, domain specific language. We can construct our own grammatical structure through DSL language. In Kotlin, there is not only one way to implement DSL, and the main way to implement it is higher-order functions (if If you don’t understand higher-order functions, don’t be afraid. I will introduce higher-order functions in a separate article later)~
I'm an Android developer, why have I never used DSL, I don't believe it!
Think about it, what do we usually need to do to introduce an open source package , we will see this code in build.gradle :
dependencies {
implementation fileTree ( dir : 'libs', include: ['*.jar'])
implementation " org.jetbrains.kotlin :kotlin-stdlib-jdk7:$ kotlin_version "
implementation ' androidx.appcompat :appcompat:1.2.0'
implementation ' androidx.core :core-ktx:1.3.1'
implementation ' androidx.constraintlayout :constraintlayout:2.0.1'
testImplementation ' junit:junit :4.12'
androidTestImplementation ' androidx.test.ext:junit :1.1.2'
androidTestImplementation 'androidx.test.espresso : espresso-core:3.3.0'
}

We all know that Gradle is a Groovy-based construction tool. The above code writing is actually the DSL function provided by Groovy.

build syntactic structure.Basic usage of DSL


Next, let's see how to use DSL to build your own grammar in Kotlin. If you want to install 13, of course we have to implement the same grammar structure as above, so let's create one~
First, we create a new class Dependency, the name can be arbitrarily named, but in order to install 13, we take the same as what we often use, declare a list array, and provide the method of adding data for the list, the class code is as follows :
class Dependency {
var libs = mutableListOf ( )
fun implementation( lib: String) {
libs.add (lib)
}
}

Next, we define a higher-order function whose parameter is the extension function of Dependency
fun dependencies( block: Dependency.() -> Unit): List {
val dependency = Dependency( )
dependency.block ()
return dependency.libs
}

The above code, as long as you understand higher-order functions, you can definitely understand. The parameters in the higher-order function are the extension functions of Dependency, so we need to initialize a Dependency first, and call the parameters through the instance to execute the incoming Lambda expression. Now, we create a new Test.kt and use it in the main method as follows:
dependencies {
implementation(" com.huanglinqing .ll " )
implementation(" com.huanglinqing .hh " )
}

How, it's similar to the method we use in build.gradle

Because the method we defined returns a List, so we can print the result, the code is as follows:
var list = dependencies {
implementation(" com.huanglinqing .ll " )
implementation(" com.huanglinqing .hh " )
}
for (text in list) {
println ("$text")
}

Running the program again, the results are as follows:
com.huanglinqing.ll _
com.huanglinqing.hh _
Process finished with exit code 0

How else can DSL be used
DSL can convert code that conforms to standard API specifications into natural language that conforms to human understanding

Let's take creating a user object as an example, create a new User.kt , and rewrite the toString method for the convenience of printing . The code is as follows:
data class User( var name: String = "", var age: Int = 0) {
override fun toString ( ): String {
return "My name is $ name , i am $age years old"
}
}

We are still writing test code in Test.kt , let's see how we can create a User object according to the API specification
val user = User( " Huanglinqing ", 25)
println (user)

The result of running is as follows:
My name is Huanglinqing , i am 25 years old
Process finished with exit code 0

So, how do we use DSL to create a User object, first we need to provide a higher-order function
fun create( block: User.() -> Unit): User {
var user = User( )
block(user)
return user
}

We define a higher-order function of type User extension function, which calls the part of the expression through block
So we can create a User object directly like this:
val user1 = create {
name = "Huang Linqing"
age = 25
}
println (user1)

We call this method a more understandable method, and the running results are consistent with the above, and will not be demonstrated here.
Anko plugin

Build syntactic structure.Anko , a framework for DSL in Kotlin. Anko uses Kotlin DSL to write Android plugins. Anko 's main function is to replace the previous way of using XML to generate UI layouts. However, Jetpack launched compose. Although there is no official version yet, it is used a lot. Many people do not understand why it is recommended to write layout code in Activity like Flutter? As we all know, the Android interface is laid out through XML. There are usually multiple layouts in an application. When the program runs, the XML is converted into Java code. It is important to focus here, even if the code is not written in Java, in the end It will still be converted to Java code, which will cause the program to be very resource-intensive. Since Anko writes the layout files directly through Java code without conversion, it is simpler and faster to use Anko to write the layout of the Android interface. So I guess, this may be one of the reasons why Jetpack launched compose.
how to use the Anko plugin. If you are interested, you can go to Github to learn about it: https://github.com/Kotlin/anko
write at the end.

Build the syntactic structure.The usage scenarios of DSL are far more than these. In fact, the premise is to use high-level functions. Many examples have mentioned the use of DSL to generate HTML code, but it is not used in business. Friends who want to know can talk to me privately. communicate. In fact, no matter what kind of technology or framework, we can't judge its quality, existence is reasonable, and promoting the project is the kingly way. Well, the basic understanding of DSL is here, let's go and install 13 happily~

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00