如果值是 `let` 常量,为什么我只能直接使用元组名称附加元组数组 -- Swift
Why can I only append an array of tuples using tuple names directly if the values are `let` constants -- Swift
这是我所说的一个例子:
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
常量。
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_expr
和 integer_literal_expr
的最终类型。 string_literal_expr
可以是 String
、StaticString
、Selector
等。
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
。并正确构造 String
和 Int
。
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
常量有String
和Int
,它没有@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
转换为[=33] =]
这是我所说的一个例子:
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
常量。
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_expr
和 integer_literal_expr
的最终类型。 string_literal_expr
可以是 String
、StaticString
、Selector
等。
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
。并正确构造 String
和 Int
。
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
常量有String
和Int
,它没有@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
转换为[=33] =]