inout parameter in closure crashes the Swift compiler

余生颓废 提交于 2019-12-05 13:30:13

问题


All I need to do is start a new project in Swift and add to main.swift

struct Foo {
  let bar: (inout baz: String) -> ()
}

When I try to build I get an error:
Command failed due to signal: Segmentation fault: 11

Am I doing anything wrong?

I thought that perhaps inout parameters in closures are not supported, but if I define a closure like so:

let baz: (inout baz: String) -> () = { baz in
  baz += "x"
  return
}

or even

var baz: (inout baz: String) -> ()?

it compiles and runs OK


回答1:


Just tested it in Swift 1.2 shipped with Xcode 6.3 beta, and it compiled successfully. So it was definitely a bug on the compiler that they solved in the last release




回答2:


@Bartek Chlebek. In the code you posted, there's one ambiguous thing (goes in bold): "let bar: (inout baz: String) -> ()". When defining your function like that, you tell the compiler that you are going to return an empty tuple. It seems like the earlier versions of the compiler didn't distinguish (must be a bug in the type inferring engine) the Void type and the "empty tuple type" (which is denoted with "()"). Here is a couple of word about the syntax of tuples: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-ID448

Possible solution: explicitly specify the return value type (which is "Void" in your case). Hope this helps.



来源:https://stackoverflow.com/questions/26746913/inout-parameter-in-closure-crashes-the-swift-compiler

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