Kotlin learning (10) to create a DSL use Kotlin

as ## 1. What is DSL
DSL (Domain Specific Language) refers to the focus on specific problem areas of computer language . Unlike general-purpose computer language (GPL), domain-specific language used only in certain areas.

DSL language allows us in a more elegant, to express and resolve problem areas more concise manner way.
In simple terms is an abstract representation of a particular issue of higher-level model program to make it more easy to understand.

DSL just a matter of problem solving external packaging solutions model, this model may be an API library, it could be a complete framework. DSL provides domain-specific modeling language ponder the problem. This allows us to be more simple, efficient solution to the problem.

A typical example is the replacement for DSL framework Anko layout XML file Android development, use it to declare on Kotlin of DSL Android UI components, rather than the traditional XML

1.1 Internal DSL
internal DSL refers to a class DSL closely related to the general purpose programming language used in the project are. It achieved based on general-purpose programming language .
One of the key features whether framework or library API to meet internal DSL is whether it has a smooth interface. This can be used to organize short of target expression had a very long expression, to make it read more naturally

1.2 DSL external
and internal DSL contrast, external DSL language is built from scratch, need to implement the parser.
Normally, we only need to implement embedded DSL, because it is easier to build, and has many of the same advantages of an external DSL

DSL characteristic 2. Kotlin support

Creating DSL in Kotlin, in general, use the following three main features:

  • Extended functions Extended Attributes
  • Lambda expressions with the recipient
  • invoke the function calling convention

In fact, it is to rewriteinvoke()

3. Implementation of a Stream Kotlin DSL collection classes

Let's create a given file returns the file name in each line of text strings streaming API, code style is like this:

fun main(args: Array<String>) {
    val lines = "src/main/resources/data.txt"
            .stream()
            .buffered()
            .reader("utf-8")
            .readLines()
     lines.forEach(::println)
}

//接下来来扩展函数:
    fun String.stream() = FileInputStream(this)

    fun FileInputStream.buffered() = BufferedInputStream(this)

    fun InputStream.reader(charset: String) = InputStreamReader(this, charset)

    fun Reader.readLines(): List<String> {
        val result = arrayListOf<String>()
        forEachLine {
            result.add(it)
        }
        return result
    }

4. The realization of a collection class SQL style DSL

Let's implement a simple SQL operations in Kotlin do DSL

//创建一个学生类
data class Student(var name: String, var sex: String, var score: Int)

 val students = listOf(
            Student("Rikka","M",90),
            Student("xiaohu","F",76),
            Student("uzi","M",105),
            Student("karsa","F",50)
        )

Next, define the sql query method:

    fun <E> List<E>.select(): List<E> = this

    //参数是一个  入参为E,出参为Boolean的函数
    fun <E> List<E>.where(predicate: (E) -> Boolean): List<E> {
        val list = this
        val result = arrayListOf<E>()
        for (e in list) {
            if (predicate(e)) {
                result.add(e)
            }
        }
        return result
    }

    fun <E> List<E>.and(predicate: (E) -> Boolean): List<E> {
        return where(predicate)
    }
Published 248 original articles · won praise 99 · Views 100,000 +

Guess you like

Origin blog.csdn.net/rikkatheworld/article/details/103039513