News for hackers. Stuff that matters.
Re:Swift: Madness of Generic Integer by pcwalton on Tuesday March 03, 2015 @06:29PM (#9141540)
Well, it's not really fair to compare an implementation that throws out memory safety to a type-safe language. Swift has pointer casts too.
Re:Swift: Madness of Generic Integer by jmgao on Tuesday March 03, 2015 @07:22PM (#9141865)
A memory safe implementation is only a bit longer:

    template<typename T>
    T getValue(std::array<char, sizeof (T)> bytes) {
        T result = 0;
        for (char byte : bytes) {
            result <<= 8;
            result |= byte;
        }
        return result;
    }
Re:Swift: Madness of Generic Integer by barrkel on Tuesday March 03, 2015 @09:50PM (#9142388)
The reason this works in C++ is because it doesn't have protocols. The operations that T is required to support in order to be a valid type argument to this function are not expressed symbolically; they need to be documented, otherwise you end up with compiler error messages that quickly get unwieldy in more complex scenarios.

In large part, that's the problem the OP is confronted with. When you make these things symbolic and first-class, unless they're extremely complete, you find holes in the system. And when they're very complete, you find yourself overwhelmed by the number and apparent complexity of what should be simple. There's an inherent conflict.

Re:Swift: Madness of Generic Integer by rian on Wednesday March 04, 2015 @12:22AM (#9142830)
C++ templates do have "protocols," they just aren't necessary. The result is that the perpetrator of a template error is ambiguous.

check out type traits: http://en.cppreference.com/w/cpp/types and std::enable_if: http://en.cppreference.com/w/cpp/types/enable_if

"concepts lite" is a proposal to add syntactic sugar for type traits as well as enhance them a bit.

in general this is how C++ does things now: first add library-level solutions as far as possible, then add language-level syntactic sugar once the usage and implementation is fully understood.

Re:Swift: Madness of Generic Integer by pcwalton on Tuesday March 03, 2015 @09:59PM (#9142413)
And note that C++ will someday add concepts, so they clearly want to move in a more Swift-like direction. A more even comparison would compare C++ with concepts to Swift.