Generic Type constraints are unsolvable

I have found a weird scenario in which generics could not be inferred correctly and I am not sure if I'm making the wrong assumptions, the compiler is acting weird or this is actually a desired outcome.

protocol FooType: Hashable, CaseIterable { }
protocol BarType: Hashable { }

protocol Resolver {
	associatedtype Foo: FooType
	associatedtype Bar: BarType

	func bar(for foo: Foo) -> Bar
}

class Provider<Foo: FooType, Bar: BarType> {

	typealias FooBarMap = [Foo: Bar]

	let map: FooBarMap

	init<R: Resolver>(handler: R) where R.Foo == Foo, R.Bar == Bar {
		self.map = Foo.allCases.reduce(into: FooBarMap()) { acc, foo in
			acc[foo] = handler.bar(for: foo)
		}
	}
}

generates the following errors:


which is a bit weird considering that the wildcards are declared as conforming to FooType.

Those errors are fixed if we remove the inheritance constraints on the Resolver's associated types:

protocol Resolver {
	associatedtype Foo
	associatedtype Bar

	func bar(for foo: Foo) -> Bar
}

Would like to start a discussion to either understand if the errors are expected in the presented scenario and if so, why.

Thank you!

1 Like

There are no errors using the latest development snapshot, so it'll be fixed in Swift 5.4.

1 Like

Did not bother to try the latest snapshot. Thank you

Terms of Service

Privacy Policy

Cookie Policy