by grumpyprole · 5y ago · 0 comments
Every C or Java programmer writes a function definition in terms of a tuple of parameters, despite not having first class tuples. Functional programming languages can also return tuples, but where are the anonymous records? These would allow defining functions with named arguments essentially for free.
So while there may be some use of structural subtyping, I don't think it's codebase-wide.
t -> bool
Much simpler and easier to consume.How would you assign a nominal type to the result of a SQL query?
So there are arguments on both sides. My point was that structural types can always easily be turned into a nominal type, but not the other way around. Therefore they should be the default.
com.google.common.base.Predicate<String> guavaPredicate = ...
java.util.function.Predicate<String> predicate = guavaPredicate::apply;For example, although Java method definitions are essentially defined in terms of tuples, function classes and objects in Java are all entirely nominal.
Recent versions of Java (Java 8+) avoid structural types but provide structural conversions + inference.
We will see with the recent introduction of named records/tuples goes in the same direction or not.
To be fair, C++ templates are not structural types, they are (together with C++ function overload resolution) an (almost) purely functional untyped programming language, producing C++ programs as output.
Of course, backward compatibility means those are optional, but the designer of the template decides. Existing libraries are all structural, but new ones will rely more on nominal meta-typing, and error messages for those are clean.
There will be pressure to retrofit older libraries, so that error messages from misuses of those get nicer too. Gripes from users who had successfully abused those libraries will be heard.
Nominal typing of generic code would be what most other languages that supports generic programming use. For example Rust has traits where one defines a named trait and must explicitly specify that a type satisfies a trait by name.
In C++ there is no such means, a concept does strictly structural checks (via constraints) to determine if a template satisfies a concept, no names are involved. In other words, a type T satisfies a concept C defined by a constraint R if and only if a T can be substituted for C's placeholder type during the evaluation of R.
For an example see std::tuple_size/tuple_element and their relation with (ironically) structured binding.
In Rust, a generic is only right or wrong, in isolation. In C++, how it behaves, including whether it compiles, depends on how you choose to use it. That makes C++ templates strictly more powerful than Rust facilities. Rust could relax some restrictions, selectively, to gain that power, and might someday.
Preventing the "compiler vomit" is a main selling point of Concepts, and it works. It is still possible to subvert it, if you work at that (then you are no worse off than before). So, don't.
let foo {x, y} = x + y
in foo {x=1, y=2}
The function "foo" above has type: { x:int, y:int } -> int
Again, a nominal type can always easily be created from a structural type.OCaml also has polymorphic variants. Which could also be called anonymous sum types.