在 kotlin 中,使用 infix
关键字修饰的函数称为中缀函数,使用是可以省略 .
和 ()
,允许以更自然(类似自然语言)的语法调用函数,这种特性可以使代码更具可读性。
1 infix 的原理
中缀函数必须满足以下条件:
- 必须是成员函数(成员方法)或扩展函数;
- 有且仅有一个参数,且不能有默认值;
- 必须显式声明
infix
关键字;
调用方法:
- 普通函数:
a.function(b)
; - 中缀函数:
a function b
(省略点号和括号,增强可读性);
示例:
class person(private val name: string) { // 成员中缀函数 infix fun say(message: string) { println("$name says $message") } } // 扩展中缀函数 infix fun int.multiply(factor: int): int = this * factor
反编译成 java 代码:
public final class person { private final string name; public final void say(@notnull string message) { intrinsics.checknotnullparameter(message, "message"); string var2 = this.name + " say " + message; system.out.println(var2); } public person(@notnull string name) { intrinsics.checknotnullparameter(name, "name"); super(); this.name = name; } }
底层实现:
- 中缀函数本质上是普通函数,但通过
infix
关键字允许省略.
和括号()
; - 编译器在语法层面给了支持;(kotlin 的很多特性都是在语法和编译器上的优化)
2 使用场景
2.1 自然语言风格的 api
适用于需要代码接近自然语言的场景,例如 dsl(领域特定语言)设计:
infix fun <t> t.shouldequal(expected: t) { if (this != expected) throw assertionerror("excepted $expected, but got $this") } // 使用 actualvalue shouldequal expectedvalue
2.2 数学或逻辑表达式
简化数学运算符或逻辑操作的表达:
infix fun int.pow(exponent: int): int = todouble().pow(exponent).toint() val result = 2 pow 3 // 等价于 2.pow(3)
2.3 键值对构造(如 to 函数)
kotlin 标准库中的 to
函数是典型的中缀函数:
val map = mapof( "name" to "eileen", "age" to 30 )
2.4 链式操作
结合中缀函数和扩展函数实现链式调用:
infix fun string.concatwith(another: string) = "$this$another" // 链式中缀调用 val message = "hello" concatwith "world" concatwith "!"
3 与普通函数相比
4 注意事项
- 参数限制:中缀函数只能有一个参数,且不能有默认值;
- 可见性:中缀函数必须是
public
或internal
(默认,模块内可见); - 优先级:中缀函数的优先级是低于算数运算符,但高于布尔运算符;
1 + 2 and 3 // 等价于 (1 + 2) and 3
5 标准库中的经典案例
kotlin 标准库广泛使用中缀函数。
5.1 to 函数
用于创建 pair
对象:
infix fun <a, b> a.to(that: b): pair<a, b> = pair(this, that)
5.2 until 函数
生成区间:
val range = 1 until 10 // 等价于 1.until(10)
5.3 集合操作
例如 step
和 downto
:
for (i in 10 downto 1 step 2) { }
到此这篇关于kotlin 中 infix 关键字的原理和使用场景的文章就介绍到这了,更多相关kotlin infix 关键字内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论