gpt4 book ai didi

string - Swift REPL 意外行为

转载 作者:可可西里 更新时间:2023-11-01 01:06:28 24 4
gpt4 key购买 nike

如果我在 Swift repl 中使用以下代码,我会得到意想不到的结果:

  1> func addA(s: String)->String { 
2. return s + "a"
3. }
4.
5. func addB(s: String)->String {
6. return s + "b"
7. }
8>
9> println(addA(""))
a
10> println(addB(""))
b
11> println(addA(addB("")))
ba
12> let p = addA(addB(""))
p: ((String)) = {
_core = {
_baseAddress = 0x0000000100500060
_countAndFlags = 2
_owner = (instance_type = Builtin.RawPointer = 0x0000000100500040)
}
}
13> println(p)
ba
14> let q = "a" + "b"
q: String = "ab"

为什么声明 p 会产生这种行为,而声明 q 却不会?

最佳答案

这里发生的一切是 REPL 让您深入了解 Swift 的一些潜在效率。 p 存储为某种函数组合。当需要评估时,它被评估。如果你不想看到香肠的制作过程,你就不应该进入香肠工厂。

关于string - Swift REPL 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27715305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com