Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "_" type mean in swift error messages?

Occasionally when using generics, I get an error message that refers to a "_" as a parameter. It doesn't appear to be documented. What does it mean?

As an example, I get the error:

Cannot convert value of type 'JawDroppingFeat<Superhero>' to closure result type 'JawDroppingFeat<_>'

when I try to compile:

protocol SuperheroType {
  typealias Superpower
}

struct JawDroppingFeat<Superhero: SuperheroType where Superhero: Arbitrary, Superhero.Superpower: Arbitrary>: Arbitrary {
  let subject: Superhero
  let superpowerUsed: Superhero.Superpower

  static var arbitrary: Gen<JawDroppingFeat<Superhero>> {
    get {
      return Gen.zip(Superhero.arbitrary, Superhero.Superpower.arbitrary)
        .map{ (x: Superhero, y: Superhero.Superpower) in
          JawDroppingFeat(subject: x, superpowerUsed: y)
        }
    }
  }
}

The Gen and Arbitrary types are from SwiftCheck and the relevant declarations are:

public struct Gen<A> {
  public static func zip<A, B>(gen1: SwiftCheck.Gen<A>, _ gen2: SwiftCheck.Gen<B>) -> SwiftCheck.Gen<(A, B)>
  public func map<B>(f: A -> B) -> SwiftCheck.Gen<B>
}

public protocol Arbitrary {
  public static var arbitrary: SwiftCheck.Gen<Self> { get }
}

I assume that the <_> is something to do with swift failing to infer the type parameter rather than an image of Chris Lattner wincing at me. But does it have a more precise (and documented) meaning?

Edit

My best current theory is that when Swift fails to infer a type parameter, instead of failing immediately, it assigns a null (_) type, which results in the actual compile error downstream at some point where an incompatible type (in my case, the parameters to .map) is passed.

like image 447
Chris Devereux Avatar asked Oct 31 '22 11:10

Chris Devereux


1 Answers

It means you have incomplete type information in a return value of parameter.

In this case the .map function is returning a generic JawDroppingFeat for which you did not specify the embedded type.

I assume you meant to write

 JawDroppingFeat<SuperHero>(subject: x, superpowerUsed: y)
like image 72
Alain T. Avatar answered Nov 08 '22 08:11

Alain T.