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

Jens Alfke jens at mooseyard.com
Mon Aug 1 20:34:48 CDT 2016

I’m pretty sure one of the reasons is because inheritance implies method inheritance, which implies being able to override methods, and that opens a big can of worms which will be familiar to any experienced C++ programmers.

Swift struct methods aren’t dynamically dispatched; they’re just function calls. But if you could subclass a struct and override methods, then the methods would need to be dynamically dispatched … but that implies that structs would need to contain vtables (or isa pointers), which makes them a lot more heavyweight. Or on the other hand, if struct methods stayed statically dispatched, then overriding them would be fraught with peril, for the same reason that overriding nonvirtual methods in C++ is generally a very bad idea.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160801/11f2817b/attachment.html>

More information about the swift-users mailing list