gpt4 book ai didi

Swift 3 默默地允许隐藏参数

转载 作者:IT王子 更新时间:2023-10-29 05:17:47 27 4
gpt4 key购买 nike

我正在切换到 Swift,我真的很不高兴以下代码在没有警告的情况下编译:

func f(_ x: inout Int?) {
var x: Int? // <-- this declaration should produce a warning
x = 105
if x! < 1000 {}
}

var a: Int? = 3
f(&a)
print("\(a)")

当然,在执行时输出 Optional(3)

在此示例中,x 局部变量隐藏了 x 函数参数。

在项目设置中打开隐藏局部变量警告(GCC_WARN_SHADOW)也不会导致产生警告。

问题:我应该如何让 Swift 3 编译器警告我这种阴影?

最佳答案

虽然您可能已经找到了有用的解决方案,但 Apple 的函数文档实际上对这种确切的使用类型有评论。您要求回答为什么代码突出显示没有警告您命名冲突,但您可能没有收到任何警告的主要原因是因为 inout 参数和所有参数不优先于在函数内初始化的变量(它们只是它们在函数内部操作时所代表的值的副本)。所以你的函数,正如我将在下面说明的那样,不考虑你传入的参数,因为你用相同的名称初始化了一个新变量。因此,根据参数管理的规则,您传递的参数将被完全忽略。我看到你的沮丧,因为在其他一些语言中这将是一个编译器错误。但是,这里有 inouts,这根本不是惯例。在 docs 中查看此处:

In-out parameters are passed as follows:

When the function is called, the value of the argument is copied. In the body of the function, the copy is modified. When the function returns, the copy’s value is assigned to the original argument. This behavior is known as copy-in copy-out or call by value result. For example, when a computed property or a property with observers is passed as an in-out parameter, its getter is called as part of the function call and its setter is called as part of the function return.

As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body. The optimized behavior is known as call by reference; it satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying. Write your code using the model given by copy-in copy-out, without depending on the call-by-reference optimization, so that it behaves correctly with or without the optimization.

Do not access the value that was passed as an in-out argument, even if the original argument is available in the current scope. When the function returns, your changes to the original are overwritten with the value of the copy. Do not depend on the implementation of the call-by-reference optimization to try to keep the changes from being overwritten. [..]

在你的情况下,如果你真的要修改你传递的参数,你会使用类似这样的东西:

If you need to capture and mutate an in-out parameter, use an explicit local copy, such as in multithreaded code that ensures all mutation has finished before the function returns.

func multithreadedFunction(queue: DispatchQueue, x: inout Int) {
// Make a local copy and manually copy it back.
var localX = x
defer { x = localX }

// Operate on localX asynchronously, then wait before returning.
queue.async { someMutatingOperation(&localX) }
queue.sync {}
}

因此,正如您在此处看到的那样,虽然 localX 不像您所做的那样被称为 x,但 localX 占用了一个完整的其他内存实例来包含数据。在这种情况下,它与 x 的值相同,但不是 x 的实例,因此它不会编译为命名错误。为了表明当您将 localX 更改为 var x = Int 时这仍然适用?就像您在函数内部所做的那样:

func f(_ x: inout Int?) {
print(x, "is x")
var x: Int? // <-- this declaration should produce a warning
print(x, "is x after initializing var x : Int?")
x = 105
print(x, "is x after giving a value of 105")
if x! < 1000 {}
}

var a: Int? = 3
f(&a)
print("\(a)", "is x after your function")

返回:

Optional(3) is x
nil is x after initializing var x: Int?
Optional(105) is x after giving a value of 105 to x
Optional(3) is x after your function

为了向您展示这到底有多远,我将使用 mohsen 所做的向您展示他的逻辑并没有完全错误,向您展示公约中的这条规则,同时我同意他没有解决缺乏的问题您问题中的代码警告。

func f(_ x: inout Int?) {
print(x, "is inout x")
var y: Int? // <-- this declaration should produce a warning
print(x, "is inout x and ", y, "is y")
x = 105
print(x, "is inout x and ", y, "is y after giving a value of 105 to inout x")
if x! < 1000 {}
}

var a: Int? = 3
f(&a)
print("\(a)", "is x after your function")

打印:

Optional(3) is inout x
Optional(3) is inout x and nil is y
Optional(105) is inout x and nil is y after giving a value of 105 to inout x
Optional(105) is x after your function

因此,正如您在第一个函数中看到的那样,您的 inout 参数和一般参数不再优先于内部包含的内容,因为它在技术上没有函数内部的初始化,这就是 inout 的目的约定本身:函数将该值保存在内存中,为该内存实例分配一个指针,然后在函数结束时将应用于该指针的任何突变应用于函数范围之外的原始变量。所以无论你在 var x: Int? 之后对它做了什么改变,当 return 被击中时,你 inout 参数中的变量都不会改变,因为你已经覆盖了分配的指针到字母 x。为了向您展示 non-inouts 情况并非如此,我们将从 x 分配一个不同的变量:

func f(_ x: Int?) {
print(x!, "is inout x")
var y: Int? // <-- this declaration should produce a warning
print(x!, "is inout x and ", y!, "is y")
x = 105
y = 100
print(x!, "is inout x and ", y!, "is y after giving a value of 105 to inout x")
if x! < 1000 {}
}

var a: Int? = 3
f(a)
print("\(a!)", "is x after your function")

返回

Playground execution failed: error: SomeTest.playground:6:7: error: cannot assign to value: 'x' is a 'let' constant
x = 105

但是,如果我返回到原始函数并将新变量重命名为与参数名称相同的指针:

func f(_ x: Int?) {
print(x, "is inout x")
var x: Int? // <-- this declaration should produce a warning
print(x, "is inout x and ")
x = 100
print(x, "is inout x and ")
if x! < 1000 {}
}

var a: Int? = 3
f(a)
print("\(a!)", "is x after your function")

我们得到:

Optional(3) is inout x
nil is inout x and
Optional(100) is inout x and
3 is x after your function

所以总而言之,inout 参数和标准参数永远不会被修改,因为在函数范围内,x 的指针完全被 Int? 覆盖。

这就是为什么你没有收到代码警告,从技术上讲,你不应该因为围绕参数的约定规定你写的不是编译冲突并且是有效代码(也许它可能不适合你的用例,但通常是这样),因此您很可能无法找到一种方法来突出显示此命名问题。

关于Swift 3 默默地允许隐藏参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39983561/

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