Bizarre Swift编译器错误:“表达式太复杂”上的字符串连接 [英] Bizarre Swift Compiler Error: "Expression too complex" on a string concatenation

查看:301
本文介绍了Bizarre Swift编译器错误:“表达式太复杂”上的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这很有趣。我已经修复它,但我想知道的原因。这里是错误: DataManager.swift:51:90:表达式太复杂,无法在合理的时间内解决;考虑将表达式分解成不同的子表达式。为什么会抱怨?

I find this amusing more than anything. I've fixed it, but I'm wondering about the cause. Here is the error: DataManager.swift:51:90: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions. Why is it complaining? It seems like one of the most simple expressions possible.

编译器指向列+);;

func tableName() -> String { return("users"); } 

func createTableStatement(schema: [String]) -> String {

    var schema = schema;

    schema.append("id string");
    schema.append("created integer");
    schema.append("updated integer");
    schema.append("model blob");

    var columns: String = ",".join(schema);

    var statement = "create table if not exists " + self.tableName() + "(" + columns + ");";

    return(statement);
}

修复是:

var statement = "create table if not exists " + self.tableName();
statement += "(" + columns + ");";

这也是可行的(通过@efischency),但我不喜欢它,因为我认为 get lost:

this also works (via @efischency) but I don't like it as much because I think the ( get lost:

var statement =create table if not exists \ self.tableName())(\(columns))

推荐答案

编译器专家 - 我不知道这个答案会改变你如何以有意义的方式改变你的想法,但我的理解是这个问题:

I am not an expert on compilers - I don't know if this answer will "change how you think in a meaningful way," but my understanding of the problem is this:

它有每次使用 + 运算符时,Swift必须搜索所有可能的重载 + + 运算符,我计算了30个重载下的 + 很多可能性,当你链接4或5 + 操作,并要求编译器推断所有的参数,你问的比它可能出现的更多第一眼。

It has to do with type inference. Each time you use the + operator, Swift has to search through all of the possible overloads for + and infer which version of + you are using. I counted just under 30 overloads for the + operator. That's a lot of possibilities, and when you chain 4 or 5 + operations together and ask the compiler to infer all of the arguments, you are asking a lot more than it might appear at first glance.

这种推断可能变得复杂 - 例如,如果添加 UInt8 Int 使用 + ,输出将是 Int

That inference can get complicated - for example, if you add a UInt8 and an Int using +, the output will be an Int, but there's some work that goes into evaluating the rules for mixing types with operators.

当你使用文字时,比如 String literals在你的例子中,编译器做将 String 字面量转换为 String 的工作,然后推断 + 运算符等的参数和返回类型的工作。

And when you are using literals, like the String literals in your example, the compiler doing the work of converting the String literal to a String, and then doing the work of infering the argument and return types for the + operator, etc.

如果表达式足够复杂 - 即,它需要编译器对参数和运算符做太多的推断 - 它退出并告诉你它退出。

If an expression is sufficiently complex - i.e., it requires the compiler to make too many inferences about the arguments and the operators - it quits and tells you that it quit.

一旦表达式达到,编译器退出一定程度的复杂性是故意的。另一种方法是让编译器尝试并执行它,看看是否可以,但这是危险的 - 编译器可以继续尝试永远,bog down或只是崩溃。所以我的理解是,有一个表达式的复杂性的静态阈值,编译器不会超越。

Having the compiler quit once an expression reaches a certain level of complexity is intentional. The alternative is to let the compiler try and do it, and see if it can, but that is risky - the compiler could go on trying forever, bog down, or just crash. So my understanding is that there is a static threshold for the complexity of an expression that the compiler will not go beyond.

我的理解是,Swift团队正在编译将使这些错误较不常见的优化。 您可以在Apple开发人员论坛上通过点击此链接了解一点。

My understanding is that the Swift team is working on compiler optimizations that will make these errors less common. You can learn a little bit about it on the Apple Developer forums by clicking on this link.

在开发者论坛上,Chris Lattner要求人们将这些错误作为雷达报告,因为他们正在积极努力修复它们。

On the Dev forums, Chris Lattner has asked people to file these errors as radar reports, because they are actively working on fixing them.

这是我如何理解它阅读了一些帖子在这里和开发论坛,但我的编译器的理解是天真的,我希望有一个更深的知识如何处理这些任务的人将扩大我在这里写的。

That is how I understand it after reading a number of posts here and on the Dev forum about it, but my understanding of compilers is naive, and I am hoping that someone with a deeper knowledge of how they handle these tasks will expand on what I have written here.

这篇关于Bizarre Swift编译器错误:“表达式太复杂”上的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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