Swift作业评估无效的原因是什么? [英] What was the reason for Swift assignment evaluation to void?

查看:102
本文介绍了Swift作业评估无效的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于历史(不是您目前对此事的看法).

This question is about HISTORY (not your current opinions on the matter).

在阅读有关放弃对Swift的增量/减量运算符的支持的文章时,我读到了这样的文字:"Swift已偏离C,因为=,+ =和其他类似赋值的操作返回Void(出于多种原因)".

While reading post about dropping support for increment/decrement operators for Swift I read such text "Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons)".

因此在过去的某个时候,出于某些原因,开发人员有意识地决定评估赋值的无效性.

So at some time in the past developers consciously decided to evaluate assignments to void for some reasons.

我正在寻找那些历史(现在)的原因.该线程几乎是关于

And I am looking for those historical (now) reasons. Pretty much as this thread is about historical reasons for Scala.

推荐答案

至少一个原因是比较操作更安全.在用C,Objective-C等语言编写代码时,您已经写了多少遍了:

At least one reason is to be safer in comparison operations. When writing in C, Objective-C, etc., how many times have you written this:

if (x = 2)

代替

if (x == 2)

较新的编译器版本针对上述情况引入了特定的警告,但是哇,多年来缺少一个等号引起了我代码中难以识别的错误.

Newer versions of compilers have introduced specific warnings for the above case, but wow has that one missing equal sign caused hard-to-identify bugs in my code over the years.

使用Swift类型系统时,这将不再是问题,因为返回的值很可能不符合BooleanType协议,但是,如果返回的值(if x = false),您仍然可能会遇到这些错误.许多Swift旨在消除人们遇到的错误的常见原因,包括这一点.

With the Swift type system, this would be less of a problem, since the returned value would most likely not comply to the BooleanType protocol, but if it did (if x = false), you might still hit these bugs. A lot of Swift is designed to eliminate common causes of bugs that people have encountered, including this one.

这在Swift编程语言书中的基本运算符" :

This is stated in the Swift Programming Language book, under "Basic Operators":

与C和Objective-C中的赋值运算符不同,赋值 Swift中的运算符本身并不返回值.以下 声明无效:

Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid:

if x = y {
    // this is not valid, because x = y does not return a value
}

此功能可防止分配运算符(=)被以下人员使用 实际打算使用等于运算符(==)时发生事故.经过 如果x = y无效,Swift可帮助您避免此类情况 代码中的错误.

This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code.

这篇关于Swift作业评估无效的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆