[swift-users] Why can't structs inherit from other structs?

Tino Heth 2th at gmx.de
Tue Aug 2 04:03:13 CDT 2016


There is no fundamental reason to disallow struct inheritance, and I started a pitch where afair one member of core agreed it would be useful — I hope there is time to add it in the next year.
There is, however, a fundamental problem with struct polymorphism:
As soon as you add members, you loose compatibility (a Point3D: Point2D can't be stored or passed where its parent is expected), and when you cross module borders, there might be issues as well.
Struct and class have to behave different in inheritance, and to disallow struct subtyping completely is just the easiest solution.
Imho it's definitely not the best solution, as it forces you to use classes whenever you want to use inheritance (I don't think that composition is superior in general).
It could be even possible to create structs that inherit from classes and vice versa.

Tino


More information about the swift-users mailing list