英文:
In Swift 5.7+ why is `if let name = name { }` preferred instead of `if let newName { }`?
问题
I’m coming back to Swift after several years. 在过去几年后,我回到了Swift。
I had a situation at work recently where I thought I was being pushed to reuse variable names. 最近在工作中,我遇到了一种情况,我觉得我被迫重用变量名。
I followed an if let
example from the latest edition of Apple’s Swift manual in which a new variable name is created. 我按照Apple的Swift手册的最新版本中的if let
示例,其中创建了一个新的变量名。
I was told that the community as a whole has opted to reuse variable names. 有人告诉我,整个社区已选择重用变量名。
I’m curious why this preferred and why declaring a new name is frowned upon. 我很好奇为什么这是首选,为什么声明新名称被看不好。
Understanding the logic behind this would help me understand why I should do it this way (after all, I’m coming from years of TypeScript where this is not OK). 了解背后的逻辑将帮助我理解为什么我应该这样做(毕竟,我来自多年使用TypeScript的经验,那里不允许这样做)。
Example from Swift manual: Swift手册中的示例:
if let name = optionalName { … }
Standard way: 标准方式:
if let name = name { … }
英文:
I’m coming back to Swift after several years. I had a situation at work recently where I thought I was being pushed to reuse variable names. I followed an if let
example from the latest edition of Apple’s Swift manual in which a new variable name is created. I was told that the community as a whole has opted to reuse variable names. I’m curious why this preferred and why declaring a new name is frowned upon. Understanding the logic behind this would help me understand why I should do it this way (after all, I’m coming from years of TypeScript where this is not OK).
Example from Swift manual:
if let name = optionalName { … }
Standard way:
if let name = name { … }
答案1
得分: 1
我认为最常见的原因是这些是常见的准则,如在官方的Ray Wenderlich Swift Style Guide中提到的。
我也相信这是由于苹果/Swift本身的偏好,根据提案SE-0345,该提案介绍了对现有可选变量进行影子化的速记法,该提案在Swift 5.7中引入。
编辑:<br/>
类似问题:https://stackoverflow.com/a/32192915/406677
英文:
I think the most common reason is that these are common guidelines, such as mentioned in the official Ray Wenderlich Swift Style Guide.
I also believe this is preferred by Apple/Swift itself given proposal SE-0345 with shorthand for shadowing existing optional variables, which was introduced in Swift 5.7.
Edit:<br/>
Similar question: https://stackoverflow.com/a/32192915/406677
答案2
得分: 1
我很好奇为什么这样做被推荐,声明新名称为什么会受到指责。
这并非如此。你仍然可以这样做
if let newName = name { ... }
在某些情况下,这可能是最合适的做法。例如,
let optionalFoo: String? = ...
...
if let foo = optionalFoo { ... }
由你决定。
在SE 345之前,人们发现他们经常写
if let foo = foo { ... }
新的语法被引入只是上面的简写,没有其他含义,尽管它似乎在与guard
一起使用时神奇地将可选值更改为非可选值。它并没有 实际上 更改变量,它用新的非可选值“复制”隐藏了原始值。
根据你的喜好或项目的风格指南使用它或不使用它。
英文:
> I’m curious why this preferred and why declaring a new name is frowned upon.
It isn't. You can still do
if let newName = name { ... }
and it is probably the most appropriate thing to do in some places. For example,
let optiionalFoo: String? = ...
...
if let foo = optionalFoo { ... }
It's up to you.
Prior to SE 345, people were finding that they were writing
if let foo = foo { ... }
quite a lot. The new syntax was introduced as a shorthand for the above, nothing more, although it does appear to magically change an optional to a non optional, particularly when used with guard
. It doesn't actually change the variable, it shadows the original with a new non optional "copy".
Use it or don't use it depending on your preference or your project's style guide.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论