当前位置:网站首页>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)
}边栏推荐
- Basic use of CTF web shrink template injection nmap
- Recursive method to construct binary tree from preorder and inorder traversal sequence
- Call pytorch API to complete linear regression
- Jmeter 的使用
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
- 追风赶月莫停留,平芜尽处是春山
- Offer harvester: add and sum two long string numbers (classic interview algorithm question)
- 【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
- opencv学习笔记五——梯度计算/边缘检测
猜你喜欢

Real time monitoring of dog walking and rope pulling AI recognition helps smart city

opencv学习笔记三——图像平滑/去噪处理

藏书馆App基于Rainbond实现云原生DevOps的实践
![[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)](/img/60/011b3ccdffa978d691436449a99e10.png)
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)

Use of JMeter

一文了解如何源码编译Rainbond基础组件

探索STEAM艺术设计中的创造力

Lua 编程学习笔记

青龙面板-今日头条

海信电视开启开发者模式
随机推荐
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
UnityHub破解&Unity破解
ZCMU--1492: Problem D(C语言)
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
调用 pytorch API完成线性回归
opencv学习笔记三——图像平滑/去噪处理
Four items that should be included in the management system of integral mall
通俗易懂单点登录SSO
海信电视开启开发者模式
buureservewp(2)
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
探索STEAM艺术设计中的创造力
CDC (change data capture technology), a powerful tool for real-time database synchronization
LeetCode简单题之字符串中最大的 3 位相同数字
发挥创客教育空间的广泛实用性
Merging binary trees by recursion
Avatary's livedriver trial experience
漏洞复现-easy_tornado
Complex network modeling (II)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)