我正在尝试使用多个值创建一个简单的if-let语句.if只有当所有可选变量都是非零时,才应执行该块,并且应将它们分配给仅存在于if块内的新let-vars(常量?),就像正常的单赋值if-let一样.
var a: String? = "A"
var b: String? // nil
if let (m, n) = (a, b) {
println("m: \(m), n: \(n)")
} else {
println("too bad")
}
// error: Bound value in a conditional binding must be of Optional type
// this of course is because the tuple itself is not an Optional
// let's try that to be sure that's the problem...
let mysteryTuple: (String?, String?)? = (a, b)
if let (m, n) …Run Code Online (Sandbox Code Playgroud) tuples parallel-assignment optional-values swift swift-playground
我懂了:
x,(y,z)=1,*[2,3]
x # => 1
y # => 2
z # => nil
Run Code Online (Sandbox Code Playgroud)
我想知道为什么z有价值nil.
我现在正在学习 Ruby 中的并行赋值运算符。当我尝试使用它来交换数组中的值时,我得到了意想不到的结果。在网上找不到这个问题的答案,希望有人能够阐明这里发生的事情。
第一个例子:
array = [1,2,3]
=> [1, 2, 3]
array[0,1] = array[1,0]
=> []
array
=> [2, 3] #thought this would be = [2,1,3]
Run Code Online (Sandbox Code Playgroud)
array[0] 去了哪里?为什么 Ruby 不交换这些值?
第二个例子:
array = [1,2,3]
=> [1, 2, 3]
array[0,1] = [1,0]
=> [1, 0]
array
=> [1, 0, 2, 3] #was expecting [1,0,3]
Run Code Online (Sandbox Code Playgroud)
为什么 Ruby 将右侧插入数组而不替换值?