Hacker News new | ask | show | jobs
by chunkyguy 1056 days ago
> A C++ class or structure becomes a standalone type in Swift. Its relationship with base C++ classes is not preserved in Swift.

C++ `class Fern: public Plant` becomes two different types in Swift `struct Plant` and `struct Fern`, how is this supposed to help anyone?

https://www.swift.org/documentation/cxx-interop/#accessing-i...

2 comments

It still allows you to use them for interop. The only functionality that would be impaired is checking for inheritance with an `as?` type check/conform.
No, this blocks a fundamental concept of OOP: Liskov Substitution Principle.

The idea is that you should be able to use a derived object anywhere the base one is required.

It impairs that principle, yes. But it doesn’t block similar patterns that give you similar end results. Swift has very flexible generics programming and type abstraction that still lets you use it with a little extra work.

Yes, it won’t be as ergonomic as the ideal, but it’s better than many binding solutions from C++ to compiled languages, while providing most of the usability.

Plus the footnote says that they plan to resolve this in future Swift versions.

It does not import the relationship, but you can still model it via protocols.
Interop isn't magic. It can't make the C++ model transparently compatible with that of Swift when the two languages deviate.