3

This code used to be legal:

protocol Flier {
    typealias Other : Flier
    func flockTogetherWith(f:Other)
}
struct Bird : Flier {
    func flockTogetherWith(f:Insect) {}
}
struct Insect : Flier {
    func flockTogetherWith(f:Insect) {}
}

Now (in Xcode 7 beta 5) it's not. What's going on here? Is this a bug? I am merely trying to ensure that adopters of Flier declare flockTogetherWith with a parameter that is some adopter of Flier. I've always been able to do that. Why is it suddenly wrong to do?

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • That code also fails to compile (with the same error message) in Xcode 7 beta 4. – Martin R Aug 07 '15 at 05:02
  • Thanks, @MartinR, I'm sure you're right. I have a lot of code examples so I don't manage to retest all of them every time there's a new beta, and nothing in the release notes led me to suspect there might be a change here, so it's very possible I skipped the examples that depend on this feature. – matt Aug 07 '15 at 13:36

1 Answers1

1

I have no idea why this should be wrong. But it's fairly easy (though annoying) to work around it: declare another protocol for Flier to adopt and use that as the constraining type:

protocol Superflier {}
protocol Flier : Superflier {
    typealias Other : Superflier
    func flockTogetherWith(f:Other)
}

It makes the code really tortured but at least it gets it past the compiler.

EDIT: Response on the dev forum from SevenTenEleven:

Having this kind of constraint isn't inherently unreasonable, but in some cases it would lead to compiler crashes. We decided to lock down on this for now. Your workaround of using a second protocol is a reasonable one.

So the change is probably to be regarded as deliberate if regrettable, and the workaround I've given is the way to go for now.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I'm waiting first for a response on the dev forums at Apple. Sometimes the Apple folks explain. – matt Aug 07 '15 at 04:11