Compiler finding unintended type

This line of code:

ParameterView(uiParameters: articulationParams, label: "Arts", height: 35.0, showNumericInput: true, parameterList: $articulations)

Gets this error message, marking the articulationsParams argument:

Cannot convert value of type '[PNParameter]' to expected argument type '[PNParameter<[Double]>]'

However, this is the declaration:

struct ParameterView<T>: View {
  var uiParameters: [PNParameter<T>] = []
...

And

let articulationParams: [PNParameter<Double>] = [
  .init(glyph: "\u{1D158}\u{1D165}\u{1D17E}", rotate: true, help: "Staccatissimo", value: 0.4),
...

I can't figure out where it's getting the [PNParameter<[Double]>] idea. When I search with Xcode, there's nothing anywhere in the project with the construction <[.+]>.

Any ideas?

First, why is ParameterView's T inferred as Array<Double>?

Second, why and how is it possible to even refer to PNParameter, if it has a generic argument?

That's what I'm asking.

Dunno, here's the declaration:

struct PNParameter<T> {
  let glyph: String
  var rotate: Bool = false
  let help: String
  let value: T
  var baseLineOffset: Double = 0.0
}

If you change the init call to be ParameterView<Double>(...) instead of just ParameterView(...), do you get a different error?

Yeah, the error goes away. But why?

I can't reproduce the issue with similar but simpler tests. How is your code different from test 1? (Your code use .init, instead of Bar(), and have default values for Foo.bars, but those don't make difference in my testing).

// test 1
struct Foo<T> {
    let bars: [Bar<T>]
}

struct Bar<T> {
    let value: T
}

let foo = Foo(bars: [Bar(value: 1), Bar(value: 2), Bar(value: 3)])

// test 2
func test2<T: CustomStringConvertible>(_ items: [S<T>]) {
    for i in items {
        print(i.value)
    }
}

test2([S(value: 1), S(value: 2), S(value: 3)])
2 Likes

A probably important difference is that my code is in ViewBuilders, which we know can sometimes be a little squirrelly about type resolution.