Hacker News new | ask | show | jobs
by WalterBright 1298 days ago
Adds:

1. convenience

2. attractive appearance

3. ubiquity

4. better error messages (because compiler knows what they are)

5. one construct instead of two (vector and span)

6. overflow behavior selectable with compiler switch

For example:

    #include <vector>
    std::vector<int> v;
vs:

    int[] v;
Many D users have remarked that it's the single best feature of D.
2 comments

I think the core issue here is that WG21 seems hell bent on refusing any new syntax if at all possible, and instead requires "generic" solutions that can be used for many problems. The result of which is these unending train wreck solutions for what should be basic, and we end up with enable_if and SFINAE. At least C++ finally has the idea of specifying an interface for a template instantiation, except of course it's absurd in its own way. Rather than specifying an API, you write "code" and a type conforms to that concept if the code "compiles". So you have no way to say "I expect this type to conform to this concept" other than using it.

The primary goal of "concepts" appears to be to mitigate the awful error messages, but even for that it fails.

For example, let's imagine Hashable from any other language, and look at a C++ concept version:

    template <typename T>
    concept Equatable = requires (const T& a, const T& b) {
      { a == b } -> std::same_as<bool>;
    };
    template <typename T>
    concept Hashable = Equatable<T> && requires (const T& a) {
      { a.hashCode() }->std::convertible_to<size_t>;
    };
and an implementation:

    struct Thing {
      size_t hashCode() const;
    };
    bool operator==(Thing, Thing);
Now how do we make sure Thing is actually going to conform to Hashable? with an assert of course, why would we want anything so gauche as declarative syntax?

    static_assert(Hashable<Thing>);
You can see the brilliant syntax explicitly disallows us specifying constraints on a concept, and instead we have to use the && expression. My personal belief here is that the banning of constraints in the template name is simply to force people to use logical composition so the people who thought of it can claim people like it.
> 3. ubiquity

Hardly. A std library update is often easier and faster to ship than a new compiler toolchain. Especially in C/C++ world, although it is moderately uniquely terrible in this regard

> 5. one construct instead of two (vector and span)

Incorrect. Vector is an owned type, while both span and your proposal are borrowed types.

> 6. overflow behavior selectable with compiler switch

That sounds an awful lot like a negative to me, not a feature. It's not generally desirable for code behavior to change depending on how it's compiled, makes debugging kinda hard.

Unless you're just referring to things like sanitizers, in which case yeah span & vector both have those same flags.

My proposal also uses [] as an owned type.

> It's not generally desirable for code behavior to change depending on how it's compiled, makes debugging kinda hard.

It only changes the behavior of what happens after the program has already failed. People want different behaviors, depending on their environment, and like having the choice.