为什么我只能使用元组名称直接追加一个元组数组,如果值为`let`常量 - Swift

时间:2015-03-18 19:31:13

标签: swift tuples

以下是我所谈论的一个例子:

typealias SomeTuple = (string: String, int: Int)

var tupleArray: [SomeTuple] = []

// Fails
// tupleArray.append(string: "Hello", int: 42)

// Works
let string = "Hello"
let num = 42
tupleArray.append(string: string, int: num)

// Fails
// var varString = "Hi Again"
// var varNum = 234
// tupleArray.append(string: varString, int: varNum)

为什么.append(tupleName: val, anotherTupleName: anotherVal)语法仅在以前将值声明为let常量时才有效?

注意:

我知道我可以用这样的额外括号包装我的元组:

tupleArray.append((string: "Hey", int: 234))

我的问题是为什么我不能用let常数做到这一点。

1 个答案:

答案 0 :(得分:3)

swiftc -dump-ast揭示了一些可能的原因。

让我们从简化代码开始:

let string = "Hello"
let num = 42
var varString = "Hi Again"
var varNum = 234 

typealias SomeTuple = (string: String, int: Int)
func foo(x:SomeTuple) {}

错误情况

foo(string: "Hello", int: 42)
  (top_level_code_decl
    (brace_stmt
      (call_expr type='()' location=test.swift:9:1 range=[test.swift:9:1 - line:9:29]
        (declref_expr type='(SomeTuple) -> ()' location=test.swift:9:1 range=[test.swift:9:1 - line:9:1] decl=test.(file).foo@test.swift:2:6 specialized=no)
        (tuple_expr type='<<error type>>' location=test.swift:9:4 range=[test.swift:9:4 - line:9:29] names=string,int
          (string_literal_expr type='<<error type>>' location=test.swift:9:13 range=[test.swift:9:13 - line:9:13] encoding=utf8 value="Hello")
          (integer_literal_expr type='<<error type>>' location=test.swift:9:27 range=[test.swift:9:27 - line:9:27] value=42))))

在这种情况下,编译器无法推断string_literal_exprinteger_literal_expr的最终类型。 string_literal_expr可以是StringStaticStringSelector等。

foo(string: varString, int: varNum)
  (top_level_code_decl
    (brace_stmt
      (call_expr type='()' location=test.swift:10:1 range=[test.swift:10:1 - line:10:35]
        (declref_expr type='(SomeTuple) -> ()' location=test.swift:10:1 range=[test.swift:10:1 - line:10:1] decl=test.(file).foo@test.swift:2:6 specialized=no)
        (tuple_expr type='(string: @lvalue String, int: @lvalue Int)' location=test.swift:10:4 range=[test.swift:10:4 - line:10:35] names=string,int
          (declref_expr type='@lvalue String' location=test.swift:10:13 range=[test.swift:10:13 - line:10:13] decl=test.(file).varString@test.swift:6:5 direct_to_storage specialized=no)
          (declref_expr type='@lvalue Int' location=test.swift:10:29 range=[test.swift:10:29 - line:10:29] decl=test.(file).varNum@test.swift:7:5 direct_to_storage specialized=no))))

在这种情况下,编译器将(string: varString, int: varNum)解释为(string: @lvalue String, int: @lvalue Int)。它与(string: String, int: Int)不匹配。

成功案例

foo(string: "Hello" as String, int: 42 as Int)
  (top_level_code_decl
    (brace_stmt
      (call_expr type='()' location=test.swift:13:1 range=[test.swift:13:1 - line:13:46]
        (declref_expr type='(SomeTuple) -> ()' location=test.swift:13:1 range=[test.swift:13:1 - line:13:1] decl=test.(file).foo@test.swift:2:6 specialized=no)
        (tuple_expr type='(string: String, int: Int)' location=test.swift:13:4 range=[test.swift:13:4 - line:13:46] names=string,int
          (coerce_expr type='String' location=test.swift:13:21 range=[test.swift:13:13 - line:13:24] writtenType=String
            (call_expr implicit type='String' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13]
              (constructor_ref_call_expr implicit type='(_builtinStringLiteral: RawPointer, byteSize: Word, isASCII: Int1) -> String' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13]
                (declref_expr implicit type='String.Type -> (_builtinStringLiteral: RawPointer, byteSize: Word, isASCII: Int1) -> String' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13] decl=Swift.(file).String.init(_builtinStringLiteral:byteSize:isASCII:) specialized=no)
                (type_expr implicit type='String.Type' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13] typerepr='<<IMPLICIT>>'))
              (string_literal_expr type='(_builtinStringLiteral: Builtin.RawPointer, byteSize: Builtin.Word, isASCII: Builtin.Int1)' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13] encoding=utf8 value="Hello")))
          (coerce_expr type='Int' location=test.swift:13:40 range=[test.swift:13:37 - line:13:43] writtenType=Int
            (call_expr implicit type='Int' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37]
              (constructor_ref_call_expr implicit type='(_builtinIntegerLiteral: Int2048) -> Int' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37]
                (declref_expr implicit type='Int.Type -> (_builtinIntegerLiteral: Int2048) -> Int' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] decl=Swift.(file).Int.init(_builtinIntegerLiteral:) specialized=no)
                (type_expr implicit type='Int.Type' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] typerepr='<<IMPLICIT>>'))
              (tuple_expr implicit type='(_builtinIntegerLiteral: Int2048)' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] names=_builtinIntegerLiteral
                (integer_literal_expr type='Int2048' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] value=42)))))))

显式广播会导致coerce_expr。它正确地构建了StringInt

 foo(string: string, int: num)
  (top_level_code_decl
    (brace_stmt
      (call_expr type='()' location=test.swift:12:1 range=[test.swift:12:1 - line:12:29]
        (declref_expr type='(SomeTuple) -> ()' location=test.swift:12:1 range=[test.swift:12:1 - line:12:1] decl=test.(file).foo@test.swift:2:6 specialized=no)
        (tuple_expr type='(string: String, int: Int)' location=test.swift:12:4 range=[test.swift:12:4 - line:12:29] names=string,int
          (declref_expr type='String' location=test.swift:12:13 range=[test.swift:12:13 - line:12:13] decl=test.(file).string@test.swift:4:5 direct_to_storage specialized=no)
          (declref_expr type='Int' location=test.swift:12:26 range=[test.swift:12:26 - line:12:26] decl=test.(file).num@test.swift:5:5 direct_to_storage specialized=no))))

let常量为StringInt,它没有@lvalue。所以它们可以按原样应用。

foo((string: varString, int: varNum))
  (top_level_code_decl
    (brace_stmt
      (call_expr type='()' location=test.swift:15:1 range=[test.swift:15:1 - line:15:37]
        (declref_expr type='(SomeTuple) -> ()' location=test.swift:15:1 range=[test.swift:15:1 - line:15:1] decl=test.(file).foo@test.swift:2:6 specialized=no)
        (paren_expr type='(SomeTuple)' location=test.swift:15:5 range=[test.swift:15:4 - line:15:37]
          (tuple_expr type='(string: String, int: Int)' location=test.swift:15:5 range=[test.swift:15:5 - line:15:36] names=string,int
            (load_expr implicit type='String' location=test.swift:15:14 range=[test.swift:15:14 - line:15:14]
              (declref_expr type='@lvalue String' location=test.swift:15:14 range=[test.swift:15:14 - line:15:14] decl=test.(file).varString@test.swift:6:5 direct_to_storage specialized=no))
            (load_expr implicit type='Int' location=test.swift:15:30 range=[test.swift:15:30 - line:15:30]
              (declref_expr type='@lvalue Int' location=test.swift:15:30 range=[test.swift:15:30 - line:15:30] decl=test.(file).varNum@test.swift:7:5 direct_to_storage specialized=no)))))))

在这种情况下,与foo(string: varString, int: varNum)相比,会插入load_expr,并将@lvalue String转换为String,将@lvalue Int转换为Int