I believe it's a false dichotomy.
My thought is still that structural supersedes nominal.
A nominal interface is just another constraint added to the list of constraints of an underlying structural interface?
I believe it's a false dichotomy.
My thought is still that structural supersedes nominal.
A nominal interface is just another constraint added to the list of constraints of an underlying structural interface?
A consequence of this is that in Rust, which has a nominal system, you can implement two traits that contain a method with the same name and are required to disambiguate at the call site. In Go you can't do that, since the method is part of the concrete type.
The additional naming constraint added to a structural interface would form a sort of namespace for methods.
I think in the comments below that someone likens this to tags in C++.
For it to work, you need to add a namespace to all the colliding methods (a simple one would be a prefix like people do in C).
A nominal system is a more constrained structural system in some ways, but the opposite is true as well, so it's not as simple as 'nominal is subset of structural'.
A nominal type system still is superseded by a structural type system.
The difference is in how a type is defined. Or what kind of constraints are in entailment said otherwise.
An interface enforces constraints. The difference here is merely that the current implementations only have either one of these type of interfaces. So for the structural type system, all methods are in the global namespace, somehow.
That's all. Because our current languages are this way doesn't mean that the two concepts cannot be reconciliated or that one is just better than the other.
> That's all. Because our current languages are this way doesn't mean that the two concepts cannot be reconciliated or that one is just better than the other.
I don't think I agree with this though, I believe they're fundamentally different. The whole point of structural constraints is that they don't need the type to be aware of them. The point of nominal constraints though is that they require the type to explicitly acknowledge them.
In an ideal situation, everyone names and types things the same ('logical') way, so structural constraints 'just work'. A type implements has_organ, and an interface requires has_organ, and the type is automatically compatible with the interface.
A nominal system is the opposite though; the type explicitly understands what a specific interface implies and formally states it.
I just can't see how there's a subset-superset relationship, or how they can somehow be reconciled.
A nominal type system doesn't necessarily enforce semantics either.
It just enforces the location of a method definition.
Seen that way, because the relation is dual, one could indeed claim that a structural interface is a nominal interface where the name constraint is elided.
But just as in subtyping, one less constraint also means bigger set.
Of course if one were to decide that an object satisfying a nominal interface doesn't satisfy the structural interface obtained by ignoring the namespace, then I'd agree as well, these concepts would be disjoint.
I don't think they are though but I don't know of a language that ever mixed both either.
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.If you have compile-time only constants you can model nominals with structural,
type Square
static const IsSquare = true
var length = 10
You can kinda hack-in subtyping, type Shape
static const Shape = true
type Square
import static from Shape
static const IsSquare = true
var length = 10The C++ 20 Standard Library provides numerous concepts which have a very different semantic requirement than the syntax they're checking. If you violate the syntactic requirement of course that'll earn you a compiler error, but if you violate the semantic requirements that's silently an ill-formed C++ program, it has no meaning whatsoever and might do absolutely anything if run.
If these were nominal, we could say, well, nobody should have deliberately implemented this inappropriate Concept, similar to an unsafe Rust trait, the act of implementation is a promise to others. But C++ Concepts aren't nominal and so there was no opportunity to do that and so in practice such deviations are likely very common despite the potentially drastic consequences.
[1] yes, concepts as an explicit language feature are new, but C++ has had de-facto concepts since Stepanov work on the original STL in the 90s.
But alas the problem here is IFNDR [Ill-formed No Diagnostic Required] so the compiler can't help you. All semantic constraints are your problem as the programmer, C++ decided that it's not the compiler's concern whether the program meets semantic constraints. Testing doesn't necessarily help at all, which is probably surprising.