2 ms·
Nominal interfaces can still be useful though, as they convey a stronger sense of intent than structural. For example, java.io.Serializable is a completely empt
by jcheng 3y ago
Nominal interfaces can still be useful though, as they convey a stronger sense of intent than structural. For example, java.io.Serializable is a completely empty interface that classes “implement” to signal that they are safe to serialize. As a structural interface, it’d be useless.
- gpderetta 3y agostructurally you can do something similar by adding some tag (in the form of a constant or nested type) to your class. For example: template<class T> concept serializable = requires { typename T::is_serializable_tag; }; void serialize(serializable auto x) {...}; struct NotSerializableClass { ... }; struct SerializableClass { using is_serializable_tag = void; ... }; serialize(NotSerializableClass{}); // error serialize(SerializableClass{}); // all good In C++, specializing a trait is also an option. So, while nominal and structural interfaces are not the same, sometimes the lines are blurred.