当前位置:网站首页>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中一键部署高可用 EMQX 集群
- JS quick start (I)
- JS cross browser parsing XML application
- WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
- 漏洞复现-Fastjson 反序列化
- Recursive method to construct binary tree from preorder and inorder traversal sequence
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- Qinglong panel -- Huahua reading
- Basic use of CTF web shrink template injection nmap
- 发挥创客教育空间的广泛实用性
猜你喜欢
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
LeetCode简单题之判断一个数的数字计数是否等于数位的值
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Record a stroke skin bone error of the skirt
使用 Nocalhost 开发 Rainbond 上的微服务应用
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
LeetCode中等题之我的日程安排表 I
快解析内网穿透为文档加密行业保驾护航
CDC (change data capture technology), a powerful tool for real-time database synchronization
随机推荐
机器人教育在动手实践中的真理
王爽 《汇编语言》之寄存器
Relevant data of current limiting
LeetCode简单题之字符串中最大的 3 位相同数字
Recursive method constructs binary tree from middle order and post order traversal sequence
Fast parsing intranet penetration escorts the document encryption industry
Uniapp mobile terminal forced update function
Wang Zijian: is the NFT of Tencent magic core worth buying?
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
快解析内网穿透为文档加密行业保驾护航
JS copy picture to clipboard read clipboard
Find the mode in the binary search tree (use medium order traversal as an ordered array)
Leetcode medium question my schedule I
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
追风赶月莫停留,平芜尽处是春山
Avatary的LiveDriver试用体验
Introduction à l'objet blob
Topic not received? Try this
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)