Kotlin 中 infix 关键字的原理和使用场景

发布于:2025-05-16 ⋅ 阅读:(12) ⋅ 点赞:(0)

在 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 注意事项

  • 参数限制:中缀函数只能有一个参数,且不能有默认值;
  • 可见性:中缀函数必须是 publicinternal(默认,模块内可见);
  • 优先级:中缀函数的优先级是低于算数运算符,但高于布尔运算符;
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 集合操作

例如 stepdownTo

for (i in 10 downTo 1 step 2) {

}

网站公告

今日签到

点亮在社区的每一天
去签到