如果值是`let` 常量,为什么我只能直接使用元组名称附加一个元组数组——Swift [英] Why can I only append an array of tuples using tuple names directly if the values are `let` constants -- Swift
问题描述
以下是我正在谈论的示例:
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)
为什么 .append(tupleName: val, anotherTupleName: anotherVal)
语法仅在值之前声明为 let
常量时才有效?
Why does the .append(tupleName: val, anotherTupleName: anotherVal)
syntax only work when the values are declared previously as let
constants?
注意:
我知道我可以用这样的额外括号将元组包装起来:
I'm aware that I could wrap my tuples in an extra parenthetical like this:
tupleArray.append((string: "Hey", int: 234))
我的问题是为什么我不必用 let
常量来做到这一点.
My question is why don't I have to do that with let
constants.
推荐答案
swiftc -dump-ast
揭示了一些可能的原因.
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) {}
错误案例
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
等.
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))))
在这种情况下,编译器将 (string: varString, int: varNum)
解释为 (string: @lvalue String, int: @lvalue Int)
.并且它与 (string: String, int: Int)
不匹配.
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)
.
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
.
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
常量是String
和Int
,没有@lvalue
.因此它们可以按原样应用.
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)))))))
在这种情况下,相对于foo(string: varString, int: varNum)
,插入了load_expr
,并转换了@lvalue String
到 String
和 @lvalue Int
到 Int
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
这篇关于如果值是`let` 常量,为什么我只能直接使用元组名称附加一个元组数组——Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!