withUnsafeMutableBytes is killing me


(Rick M) #1

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)
}

func
foo(_ data: UnsafeMutableRawPointer!)
{
}

func
lgs_error(_ message: UnsafeMutablePointer<Int8>!,
      _ message_capacity: Int,
      _ message_size: UnsafeMutablePointer<Int>!) -> Int
{
}
---------------------------

--
Rick Mann
rmann@latencyzero.com


(Ole Begemann) #2

The withUnsafeMutableBytes method has two generic parameters, ResultType and ContentType:

mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body:

(UnsafeMutablePointer <dash-apple-api://load?request_key=hsAKsXE560&language=swift><ContentType>) throws -> ResultType) rethrows -> ResultType|

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)
}

func
foo(_ data: UnsafeMutableRawPointer!)
{
}

func
lgs_error(_ message: UnsafeMutablePointer<Int8>!,
      _ message_capacity: Int,
      _ message_size: UnsafeMutablePointer<Int>!) -> Int
{
}
---------------------------


(Rick M) #3

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:

mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType

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)
}

func
foo(_ data: UnsafeMutableRawPointer!)
{
}

func
lgs_error(_ message: UnsafeMutablePointer<Int8>!,
      _ message_capacity: Int,
      _ message_size: UnsafeMutablePointer<Int>!) -> Int
{
}
---------------------------

--
Rick Mann
rmann@latencyzero.com


(Ole Begemann) #4

Not the ResultType, you mean, but the input type, right?

Yes, sorry, I meant ContentType.

···

On 25.04.2017 12:24, Rick Mann via swift-users wrote:

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:

mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType

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)
}

func
foo(_ data: UnsafeMutableRawPointer!)
{
}

func
lgs_error(_ message: UnsafeMutablePointer<Int8>!,
      _ message_capacity: Int,
      _ message_size: UnsafeMutablePointer<Int>!) -> Int
{
}
---------------------------