我正在切换到 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/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些