Kotlin 语言

Kotlin 不是 Swift on JVM

  • Swift 有 sum type, Kotlin 用 sealed class 来模拟, 但易用性真的不敢恭维, 写起来要加不少莫名其妙的字符.
// Kotlin
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()

fun eval(expr: Expr): Double = when(expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    NotANumber -> Double.NaN
}

fun main() {
    println(eval(Sum(Const(1.0), Const(2.0)))) // --> 3.0
}

这是 Swift 的写法 :

// Swift
indirect enum Expr {
    case nan
    case const(Double)
    case sum(Expr, Expr)
}

func eval(_ expr: Expr) -> Double {
    switch (expr) {
    case .nan:
        return Double.nan
    case let .const(x):
        return x
    case let .sum(x, y):
        return eval(x) + eval(y)
    }
}

print(eval(.sum(.const(1.0), .const(2.0)))) // --> 3.0
  • Swift 有元组, Kotlin 没有.
// Kotlin
val (state, msg) = 404 to "not found."
val (state, msg) = Pair(404, "not found.")
println("error: $state ($msg)")

这是 Swift (以及所有正常语言) 的写法 :

// Swift
let (state, msg) = (404, "not found.")
print("error: \(state) (\(msg))")
  • Swift 有良好的数组 / 词典字面量
// Kotlin
val cube = arrayListOf(
    arrayListOf(1, 2, 3),
    arrayListOf(4, 5, 6),
    arrayListOf(7, 8, 9)
)

Swift 里是这样的

// Swift
let cube = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  • Swift 能自定义运算符 (包括前缀, 中缀和后缀, 优先级), Kotlin 不知道是不是觉得这个东西复杂度太高, 只能允许中缀表示法.
// Kotlin
infix fun Double.sumOfSquare(rhs: Double): Double {
   return this * this + rhs * rhs
}

val x = 2.0 sumOfSquare 3.0

Swift 中是这样的

// Swift
infix operator *+*
func *+*(lhs: Double, rhs: Double) -> Double {
    return lhs * lhs + rhs * rhs
}

2.0 *+* 3.0
  • Swift 有 inout 参数, 可以直接改变函数的参数, 而 Kotlin (很多语言也) 没有
// Swift
infix operator <->

func <-><T>(lhs: inout T, rhs: inout T) {
    return swap(&lhs, &rhs)
}

var (a, b) = (1, 2)
a <-> b
print("a, b = \(a), \(b)") // a, b = 2, 1
  • Swift 中运算符可以直接作为高阶函数参数
// Kotlin
val sum = arrayListOf(1, 4, 5, 2, 3).reduce { x, y -> x + y } // 15

Swift 里可以直接用裸的 +

// Swift
let sum = [1, 4, 5, 2, 3].reduce(0, +) // 15

而 Kotlin 里那些语法特性, Swift 里当然有 :

  • 扩展方法 extension
extension Int {
    func times(_ fn: () -> Void) {
        (1...self).forEach { _ in fn() }
    }
}

3.times {
    print("Hi!")
}
  • 高阶函数, 语法和 Kotlin 十分类似.
(1...100).map { $0 * $0 }
         .filter { $0 % 2 == 1 }
  • 空安全和 Optional
let x: Int? = nil

x?.distance(to: 5) // nil
let y = x ?? 0 // 默认值

if let it = x { // if let
    // ...
}

                                                                        需要更多教程,微信扫码即可

                                                                                

                                                                                         👆👆👆

                                                        别忘了扫码领资料哦【高清Java学习路线图】

                                                                     和【全套学习视频及配套资料】
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值