Swift playground显示错误的执行次数(forEach on array)(Swift playground shows wrong number of executions for st

编程入门 行业动态 更新时间:2024-10-21 23:02:12
Swift playground显示错误的执行次数(forEach on array)(Swift playground shows wrong number of executions for statement (forEach on array))

我一直在探索仿函数,我在理解forEach函数在幕后做什么时遇到了一些麻烦。 例如,当我将其输入游乐场时:

let array = [1] // [1] array.forEach { $0.value } // (3 times) array.forEach { _ in print("hello") } // (2 times)

当我展开(3 times)或(2 times)它只是显示()

首先,为什么在1个元素的数组上有多次执行,为什么两个forEach计算的执行次数不同?

I've been exploring functors and I'm having a little trouble understanding what the forEach functor is doing behind the scenes. For example when I input this into a playground:

let array = [1] // [1] array.forEach { $0.value } // (3 times) array.forEach { _ in print("hello") } // (2 times)

When I expand the (3 times) or (2 times) it simply shows ()

For one, why are there multiple executions on an array of 1 element, and why do the two forEach computations vary in number of executions?

最满意答案

这是一个非常令人困惑的情况。

让我们先考虑第二个forEach :

array.forEach { _ in print("hello") } // (2 times)

该行的不同部分在不同的时间执行,Swift将每个时间作为单独的执行进行计数。 第一次是它调用array.forEach ,第二次是在调用forEach时,它执行在匿名函数体内print的调用。 如果我们输入换行符,我们可以看到Swift只执行一行并报告其“值”:

array.forEach { _ in // [1] print("hello") // () }

我们也可以尝试将匿名函数放在变量中:

let p: (Int) -> () = { _ in print("hello") } // (2 times) array.forEach(p) // [1]

在上面,Swift执行let p line的一部分来创建匿名函数并将其存储在p ,稍后该行的另一部分将在函数体内调用print 。

Swift报告print行的值是()因为forEach的参数必须是一个返回()的函数(空元组,也就是Void )。 由于print已经返回() ,Swift只是让它成为行的值。

在我们回过头来考虑你的第一个forEach示例之前,让我们考虑另一个例子:

print("hello"); print("goodbye") // (2 times)

Swift说这行执行了两次,因为该行上的每个单独的语句都算作一个单独的执行。

那么现在让我们考虑你的第一个例子:

array.forEach { $0.value } // (3 times)

让我们用换行符来试试吧:

array.forEach { // [1] $0.value // (2 times) }

好的,所以forEach调用本身算作一次执行,正如预期的那样。 但Swift声称它正在执行两次匿名函数的主体。 为什么?

回想一下, forEach的参数必须是一个返回()的函数。 但$0.value的类型不是() ; 它是内部类型Builtin.Int64 。 因此Swift在行的末尾插入另一个语句,返回() 。 实际上,Swift的行为就像你写的那样:

array.forEach { // [1] $0.value; () // (2 times) }

我们可以通过明确地向函数添加另一行来证明:

array.forEach { // [1] $0.value // <<<opaque type>>> () }

现在,Swift认为每行都按预期执行一次。

This is quite a confusing situation.

Let's consider the second forEach first:

array.forEach { _ in print("hello") } // (2 times)

Different parts of that line are executed at different times, and Swift counts each of those times as a separate execution. The first time is when it calls array.forEach, and the second time is inside that call to forEach when it executes the call to print in the body of the anonymous function. If we put newlines in, we can see that Swift only executes each line once and reports its “value”:

array.forEach { _ in // [1] print("hello") // () }

We can also try putting the anonymous function in a variable:

let p: (Int) -> () = { _ in print("hello") } // (2 times) array.forEach(p) // [1]

Above, Swift executes part of the let p line once to create the anonymous function and store it in p, and another part of the line later to call print inside the body of the function.

Swift reports that the value of the print line is () because forEachs argument must be a function that returns () (the empty tuple, aka Void). Since print already returns (), Swift just lets that be the value of the line.

Before we go back to consider your first forEach example, let's consider one other example:

print("hello"); print("goodbye") // (2 times)

Swift says this line executes two times because each individual statement on the line counts as a separate execution.

So now let's consider your first example:

array.forEach { $0.value } // (3 times)

Let's try it with newlines:

array.forEach { // [1] $0.value // (2 times) }

OK, so the forEach call itself counts as one execution, as expected. But Swift claims it is executing the body of the anonymous function twice. Why?

Recall that forEach's argument must be a function that returns (). But the type of $0.value is not (); it is the internal type Builtin.Int64. So Swift inserts another statement at the end of the line, to return (). Effectively, Swift acts like you wrote this:

array.forEach { // [1] $0.value; () // (2 times) }

And we can prove that by explicitly adding another line to the function:

array.forEach { // [1] $0.value // <<<opaque type>>> () }

Now Swift thinks each line is executed once, as expected.

更多推荐

本文发布于:2023-07-04 11:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:次数   错误   forEach   playground   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!