The following playground reproduces an issue I'm having, in that the code won't compile depending on the content of the closure. In fact, an empty closure is fine, but when I try to call certain things, it's not.
I figure it has something to do with the type inference for inPointer, but I can't figure out what it needs to work.
···
---------------------------
import Foundation
// OKAY:
var msg = Data(capacity: 123456)
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
foo(inPointer)
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
var s: Int
lgs_error(inPointer, 123456, &s)
}
In your examples, the type checker can't infer the type of ResultType. You'll have to state it explicitly by specifying the type of the closure's argument. For example:
msg.withUnsafeMutableBytes {
(inPointer*: UnsafeMutablePointer<UInt8>*) -> Void in
// ...
}
···
On 25.04.2017 10:45, Rick Mann via swift-users wrote:
The following playground reproduces an issue I'm having, in that the code won't compile depending on the content of the closure. In fact, an empty closure is fine, but when I try to call certain things, it's not.
I figure it has something to do with the type inference for inPointer, but I can't figure out what it needs to work.
---------------------------
import Foundation
// OKAY:
var msg = Data(capacity: 123456)
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
foo(inPointer)
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
var s: Int
lgs_error(inPointer, 123456, &s)
}
Not the ResultType, you mean, but the input type, right? Yeah, I finally figured that out, although it doesn't explain another situation I'm experiencing that I didn't include in the post.
However, that doesn't explain why it can't infer it in the last example.
···
On Apr 25, 2017, at 02:58 , Ole Begemann <ole@oleb.net> wrote:
The withUnsafeMutableBytes method has two generic parameters, ResultType and ContentType:
In your examples, the type checker can't infer the type of ResultType. You'll have to state it explicitly by specifying the type of the closure's argument. For example:
On 25.04.2017 10:45, Rick Mann via swift-users wrote:
The following playground reproduces an issue I'm having, in that the code won't compile depending on the content of the closure. In fact, an empty closure is fine, but when I try to call certain things, it's not.
I figure it has something to do with the type inference for inPointer, but I can't figure out what it needs to work.
---------------------------
import Foundation
// OKAY:
var msg = Data(capacity: 123456)
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
foo(inPointer)
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
var s: Int
lgs_error(inPointer, 123456, &s)
}
In your examples, the type checker can't infer the type of ResultType. You'll have to state it explicitly by specifying the type of the closure's argument. For example:
On 25.04.2017 10:45, Rick Mann via swift-users wrote:
The following playground reproduces an issue I'm having, in that the code won't compile depending on the content of the closure. In fact, an empty closure is fine, but when I try to call certain things, it's not.
I figure it has something to do with the type inference for inPointer, but I can't figure out what it needs to work.
---------------------------
import Foundation
// OKAY:
var msg = Data(capacity: 123456)
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
foo(inPointer)
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
var s: Int
lgs_error(inPointer, 123456, &s)
}