当前位置:网站首页>Kotlin common standard functions
Kotlin common standard functions
2022-06-25 09:52:00 【seevc】
1.apply function
It can be regarded as a configuration function , Pass in a receiver , Configure it to use by calling a series of functions , If apply Provide lambda expression , The configured receiver will be returned .apply Can make every configuration function act on the receiver , This behavior is also called correlation scope . The configuration function is an implicit call to the receiver .
fun main() {
var file = File("D://file.txt").apply {
setReadable(true)
setWritable(true)
}
}
2.let function
let Function can make a variable act on lambda In the expression , Give Way it Keywords can reference it .
| function | Parameters | Return value |
|---|---|---|
| let | Receiver object | lambda The last line of the expression |
| apply | – | Current recipient |
fun main() {
val ret = listOf<Int>(5,2,1).last().let {
it.shl(2)
}
println(ret)
}
3.run function
From the perspective of scope behavior ,run Functions and apply similar , But the result is different ,apply Return to recipient , and run The return is lambda The execution result of the expression , From the return value, it looks like let. in addition ,run Functions can also execute references to functions .
fun main() {
val ret = "Welcom,zhh".run {
contains("zh")
length
}
println(ret)
// A reference to an executing function
20.run(::getAge)
.run(::getName)
.run(::println)
}
private fun getAge(age:Int) = " Age :$age"
private fun getName(name:String) = " name :$name"
4.with function
yes run Variations of functions , Their behavior is consistent . But the method of calling is different , Use with You need to pass the value parameter as the first parameter .
fun main() {
// The value parameter is passed in as the first parameter
val ret = with("Hello,Kotlin"){
length > 5
}
println(ret)
}
5.also function
also and let similar , Pass the value parameter in the receiver to lambda expression , But the return value is different ,also The receiver object is returned , and let The return is lambda The execution result of the expression . In view of this difference ,also Often works on the same original object
fun main() {
var list : List<String>
File("D://file.txt")
.also {
println(it.name)
}.also {
it.setReadable(true)
}.also {
list = it.readLines()
}
println(list)
}
6.takeIf function
takeIf Function is different from other standard functions , It needs judgment lambda Conditional expression provided in expression , give true or false result , If the judgment is true, Then the receiver object is returned , Otherwise return to null. This function is mainly used to judge whether a certain condition is satisfied first , In the scenario of execution . Similar to If
fun main() {
val ret = File("D://file.txt").takeIf {
it.exists() && it.canRead()
}?.readText()
println(ret)
}
7.takeUnless function
yes takeIf Auxiliary function of , Only the given condition will result in false,takeUnless Will return the original recipient object .
fun main() {
// When the conditions are not met , To execute the follow-up
val ret = File("D://file.txt").takeUnless {
it.isHidden
}?.readText()
println(ret)
}
Welcome to leave a message for us to exchange and learn from each other !
Sample source address kotlin_demo
边栏推荐
- [zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most
- Flutter dialog: cupertinoalertdialog
- How to "transform" small and micro businesses (I)?
- The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
- Chitubox micromake l3+ slicing software configuration correspondence
- Get started quickly with jetpack compose Technology
- TLAB mechanism of JVM object memory allocation and TLAB process in G1
- Flutter Gaode map privacy compliance error
- 广发证券靠谱吗?是否合法?开股票账户安全吗?
- Fluent creates, reads and writes JSON files
猜你喜欢

With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire

2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
![[MySQL learning notes 21] storage engine](/img/3a/a3cd573281efc689cafdb7d7562ce0.png)
[MySQL learning notes 21] storage engine

Learning notes of rxjs takeuntil operator

manhattan_slam环境配置

Applet cloud development joint table data query and application in cloud function

Download the arm64 package of Debian on X86 computer

可穿戴设备或将会泄露个人隐私
![[wechat applet full stack development course] course directory (mpvue+koa2+mysql)](/img/1c/ab39cf0a69d90a85665a099f5dbd26.jpg)
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
随机推荐
js工具函数,自己封装一个节流函数
x86电脑上下载debian的arm64的包
Unique Wulin, architecture selection manual (including PDF)
Is it safe to open an account online? Who can I ask?
Creo makes a mobius belt in the simplest way
PMP考试多少分算通过?
2021mathorcupc topic optimal design of heat dissipation for submarine data center
Fcpx quickly add subtitles | Final Cut Pro import fcpxml subtitle file does not match the video time? I got it in code
【mysql学习笔记21】存储引擎
[matlab] image binarization (imbinarize function)
2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
Wearable devices may reveal personal privacy
汇付国际为跨境电商赋能:做合规的跨境支付平台!
Summarize two methods of configuring pytorch GPU environment
Neat Syntax Design of an ETL Language (Part 2)
瑞吉外卖项目(二)
富时A50开户什么地方安全
力扣-104. 二叉树的最大深度
Flutter replaces the default icon of Gaud positioning
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0