当前位置:网站首页>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)
}
边栏推荐
- 在Rainbond中实现数据库结构自动化升级
- Recursive construction of maximum binary tree
- Call pytorch API to complete linear regression
- [quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
- Rainbond结合NeuVector实践容器安全管理
- Openjudge noi 2.1 1752: chicken and rabbit in the same cage
- Empire CMS collection Empire template program general
- Lua 编程学习笔记
- Introduction à l'objet blob
- 解读创客思维与数学课程的实际运用
猜你喜欢
Vulnerability recurrence fastjson deserialization
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
藏书馆App基于Rainbond实现云原生DevOps的实践
【踩坑系列】uniapp之h5 跨域的问题
王爽 《汇编语言》之寄存器
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
eBPF Cilium实战(2) - 底层网络可观测性
随机推荐
BiSeNet的特点
Interactive book delivery - signed version of Oracle DBA work notes
漏洞複現-Fastjson 反序列化
Codeforce c.strange test and acwing
Vulnerability recurrence easy_ tornado
Search for an element in a binary search tree (BST)
解析机器人科技发展观对社会研究论
opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
Zcmu--1492: problem d (C language)
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
Qinglong panel -- finishing usable scripts
藏书馆App基于Rainbond实现云原生DevOps的实践
Application of slip ring of shipborne radar antenna
Wang Zijian: is the NFT of Tencent magic core worth buying?
发挥创客教育空间的广泛实用性
LeetCode简单题之判断一个数的数字计数是否等于数位的值
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Network learning (III) -- highly concurrent socket programming (epoll)
Dedecms collects content without writing rules
让Livelink初始Pose与动捕演员一致