当前位置:网站首页>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)
}边栏推荐
猜你喜欢

The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number

Jmeter 的使用

Application of slip ring of shipborne radar antenna

青龙面板-今日头条

Rainbond 5.7.1 支持对接多家公有云和集群异常报警

eBPF Cilium实战(1) - 基于团队的网络隔离

Network learning (I) -- basic model learning

【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习

Empire CMS collection Empire template program general

Myabtis_ Plus
随机推荐
Call pytorch API to complete linear regression
Introduction à l'objet blob
Recursive method to verify whether a tree is a binary search tree (BST)
It's too true. There's a reason why I haven't been rich
复杂网络建模(三)
Avatary's livedriver trial experience
jeeSite 表单页面的Excel 导入功能
基于Pytorch 框架手动完成线性回归
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
Unityhub cracking & unity cracking
Relevant data of current limiting
Complex network modeling (III)
Network learning (II) -- Introduction to socket
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
互动送书-《Oracle DBA工作笔记》签名版
Use of JMeter
Blob object introduction
Example of file segmentation
Bugku CTF daily one question chessboard with only black chess
快解析内网穿透助力外贸管理行业应对多种挑战