当前位置:网站首页>Domain specific language / DSL in kotlin
Domain specific language / DSL in kotlin
2022-07-07 08:14:00 【yu-Knight】
class Context {
val info = "yuknight"
val name = "AAA"
fun toast(str: String) = println("toast:$str")
}
inline fun Context.myApply(lambda: Context.(String) -> Unit): Context {
lambda(info)
return this
}
inline fun File.applyFile(action: (String, String) -> Unit): File {
setWritable(true)
setReadable(true)
action(this.name, this.readLines()[0])
return this
}
fun main() {
// Actually myApply Function is DSL Programming paradigm , Define rules such as input and output
//1. Define the whole lambda Rules and standards , Input Must be Context This class , To call myApply function , Held in anonymous functions it and this
//2. Define the whole lambda Rules and standards , Output Always return Context In itself , So you can chain call
// then main The function can be based on DSL Programming mode standard rules to write specific implementation , This is it. DSL Programming paradigm
val context = Context().myApply {
//it == String == "yuknight"
println(" my it:$it, this:$this")
toast("abcdefghijklmn")
toast(it)
toast(name)//this.name
true
}.myApply { }.myApply { }
println(" Always output yes :" + context.name)
println()
// Actually applyFile function , Namely DSL Programming paradigm , Define rules such as input and output
//1. Define the whole lambda Rules and standards , Input File, Actually applyFile function , Held in anonymous functions fileName and data
//2. Define the whole lambda Rules and standards , Output File, So you can chain call
val file: File = File("D:\\a.txt")
.applyFile { fileName, data ->
println(" file name :$fileName, The content is :$data")
true// The last line does not affect the return value
}.applyFile { a, b -> }.applyFile { a, b -> }
println(" Always output File In itself :"+ file.name)
}边栏推荐
- 漏洞复现-easy_tornado
- Wang Zijian: is the NFT of Tencent magic core worth buying?
- WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
- Lua 编程学习笔记
- Interactive book delivery - signed version of Oracle DBA work notes
- Zcmu--1492: problem d (C language)
- 快解析内网穿透助力外贸管理行业应对多种挑战
- Record a stroke skin bone error of the skirt
- 青龙面板--花花阅读
- Interview questions (CAS)
猜你喜欢
随机推荐
buureservewp(2)
探索STEAM艺术设计中的创造力
太真实了,原来自己一直没有富裕起来是有原因的
饥荒云服管理脚本
复杂网络建模(二)
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
Search for an element in a binary search tree (BST)
offer收割机:两个长字符串数字相加求和(经典面试算法题)
Empire CMS collection Empire template program general
漏洞复现-Fastjson 反序列化
Recursive method to construct binary tree from preorder and inorder traversal sequence
【雅思口语】安娜口语学习记录 Part3
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
Real time monitoring of dog walking and rope pulling AI recognition helps smart city
opencv学习笔记三——图像平滑/去噪处理
【踩坑系列】uniapp之h5 跨域的问题
Application of slip ring of shipborne radar antenna
Zsh shell adds automatic completion and syntax highlighting
The element with setfieldsvalue set is obtained as undefined with GetFieldValue








