当前位置:网站首页>Kotlin domain specific language (DSL)
Kotlin domain specific language (DSL)
2022-07-24 10:11:00 【Deciduous ex】
Kotlin Domain specific language (DSL)
DSL What is it?
For me DSL Is a familiar and unfamiliar word , I always feel that I often see it, but I don't know what it is . For ease of understanding, it is necessary to mention programing language , We are all familiar with this , such as Java、kotlin All belong to programing language , and DSL Its full name is “ Domain specific language ”. The following is a comparison of the two :
- programing language : There is a set of sufficient and perfect abilities to solve almost all problems that can be solved by computers .
- DSL: Focus on specific tasks , Or in the field , And give up functions irrelevant to this field .
It can be seen that , A language that is specific to the existence of a special function and can only complete this part of the function is called Domain specific language . Common are DSL Yes SQL And regular expressions .
Embedded DSL
Embedded DSL As kotlin A new feature of , It can facilitate our development . Then why use inline DSL Instead of using it directly , With SQL For example .
These languages in order to achieve their goals more effectively , Passing will reduce the functions they provide , So when you need to execute a SQL At the time of statement , Don't declare a class or method , every last keyword It represents the operation and type to be performed , Each type of operation has its own unique Of grammar And keyword sets for specific tasks .
And often DSL Language tends to be declarative , And general programming language contrary , Mostly orders .
This leads to direct use DSL There is a drawback : They are difficult to interact with host applications that use a common programming language combination Get up and use . Simply put, the grammar is different , It needs to be passed in string and other forms , Inconvenient Correct mistakes in time .
Example
Anko SQLite(SQL)
fun getUsers(db: ManagedSQLiteOpenHelper): List<User> = db.use {
db.select("Users")
.whereSimple("family_name = ?", "John")
.doExec()
.parseList(UserParser)
}Anko Layouts(XML)
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!") }
}
}summary
kotlin Embedded with DSL It makes the writing of some special domain languages easier and more unified . When proficient, you can even stop writing XML File layout , No more hard to remember and inconvenient to debug SQL sentence .( Although I am still used to using XML Write layout )
The principle of its implementation is also very simple , In fact, the common method is based on lambda The form of expression completes the highly concise code style . To put it bluntly, it is to complete the above functions in the form of code , But these codes have been kotlin Deeply encapsulated and optimized .
边栏推荐
- Value on the object Z rotation synchronization panel in unity
- [STM32 learning] (14) two 74HC595 controls four nixie tube displays
- [STM32 learning] (17) STM32 realizes LCD12864 display serial implementation
- 多表查询之子查询_单行单列情况
- Looting (leetcode-198)
- Arduino- how to light the LED?
- Where is the bitbucket clone address
- 2022, enterprise unified process platform design and integration specifications refer to thubierv0.1
- Redis configuration serialization
- The best time to buy and sell stocks includes handling charges (leetcode-714)
猜你喜欢

Implementation and traversal of binary tree and binary tree sorting tree

Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K

Ribbon's loadbalancerclient, zoneawareloadbalancer and zoneavoidancerule are three musketeers by default
![[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)](/img/fd/4290525914b5146fe0eb653517fef9.png)
[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)
![[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display](/img/f2/6fcd4b2e747b4ceb52a52eda0c1af4.png)
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display

二叉树、二叉树排序树的实现及遍历

Do you really understand the concept of buffer? Take you to uncover the buffer zone~

Ask you to build a small program server
![[STM32 learning] (12) STM32 realizes LCD1602 simple static reality](/img/78/954ebaae0cce5d9387e7032fa85e60.png)
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality

Tree array-
随机推荐
Source insight 3.5 comment garbled
unity中物体z旋转同步面板上的数值
note: expected ‘void * (***)(void ***)’ but argument is of type ‘void (*)(void *)’
What is the cloud native mid platform business architecture?
Simple parsing JSON strings with regular expressions
[STM32 learning] (6) use of serial port 1 (usart1)
What did zoneawareloadbalancer of ribbon and its parent class do?
Tree array-
Development history of the first commercial humanoid biped robot in China
Arduino serial port information reading and output
Tencent 5g innovation center was established, laying out key directions such as unmanned ports, smart mines and E-sports events
[sword finger offer II 115. reconstruction sequence]
Openstack network neutron knowledge point "openstack"
缓冲区的概念真的理解么?带你揭开缓冲区的面纱~
[STM32 learning] (9) stm32f1 general timer realizes simple breathing lamp
This article takes you to understand the dynamic memory allocation of C language
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
The best time to buy and sell stocks (leetcode-121)
System a uses window.open to call system B, and system B carries data back to system a after processing the business
Write a simple memo using localstorage