Why can I only append an array of tuples using tuple names directly if the values are `let` constants — Swift

会有一股神秘感。 提交于 2019-12-22 10:55:02

问题


Here's an example of what I'm talking about:

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)

Why does the .append(tupleName: val, anotherTupleName: anotherVal) syntax only work when the values are declared previously as let constants?

Note:

I'm aware that I could wrap my tuples in an extra parenthetical like this:

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

My question is why don't I have to do that with let constants.


回答1:


swiftc -dump-ast unveils some possible reasons.

Let's start with simplified code:

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

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

Error cases

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))))

In this case, the compiler cannot infer final type of string_literal_expr and integer_literal_expr. string_literal_expr can be String, StaticString, Selector or such.

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))))

In this case, the compiler interpreted (string: varString, int: varNum) as (string: @lvalue String, int: @lvalue Int). And it does not match with (string: String, int: Int).

Success cases

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)))))))

Explicit cast causes coerce_expr. And it constructs String and Int correctly.

 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 constants are String and Int, it does not have @lvalue. So they can be applied AS IS.

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)))))))

In this case, compared to foo(string: varString, int: varNum), load_expr is inserted, and it converts @lvalue String to String, and @lvalue Int to Int



来源:https://stackoverflow.com/questions/29131091/why-can-i-only-append-an-array-of-tuples-using-tuple-names-directly-if-the-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!