They both support a meaningful left-bitshift operation, which is what the author wanted to abstract over.
"The CPU handles it differently" isn't by itself a legitimate argument against abstraction.
Note that this doesn't depend on your CPU, but rather on how integers and their byte representations are specified in the language.
I think it's quite reasonable to expect the programmer to know what that means assuming they are writing bitfiddling code like this.
> It's just so much easier and faster to do it like Go
Would you say the same thing about registers versus stack-allocated memory? ISAs treat registers and stack-allocated memory very differently, yet virtually nobody wants the "register" keyword in C back and nobody is asking for a similar keyword in Go. (In fact, Go even abstracts over heap memory in the same way.)
"The CPU does it differently" is not a valid argument per se against abstraction.
> It's faster to write (because you don't need to figure out how to do in a generic way)
And every time you copy/paste code, it's easier and faster to make stupid copy/paste mistakes either the first time, or when you change one version, but not the others.
But yes, the "problem" of just writing generic code involving things that are numbers, and letting your compiler or interpreter figure out the most efficient way to represent them in memory, is not really a problem; it's been solved by plenty of languages.
template<typename T>
T getValue(std::array<char, sizeof (T)> bytes) {
return *reinterpret_cast<T *>(bytes.data());
} template<typename T>
T getValue(std::array<char, sizeof (T)> bytes) {
T result = 0;
for (char byte : bytes) {
result <<= 8;
result |= byte;
}
return result;
}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.
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.
err := binary.Read(bytes.NewReader(b), binary.LittleEndian, &i) template<typename T>
typename std::enable_if<std::is_trivial<T>::value, T>::type
getValue(std::array<char, sizeof(T)> bytes) {
T toret;
std::memcpy(&toret, bytes.data(), sizeof(toret));
return toret;
}
there is no-performance hit compared to your function. this is a type-safe and well-defined version of the function above.btw, was your usage of trivial a pun? if so, that's amazing. we need more type traits puns.
I do, however, agree with you in general—it seems like he's trying to re-implement generic serialization. Otherwise, it's difficult to see what a 'Bit' (for instance) has in common with an Int and not, for instance, a double.
(Remove "like?")
You shouldn't use generics to magically get the compiler to magically do low level things for you. IMO, generics (specifically C++ style generics) should be used to manage things at fairly high levels of abstraction for fairly domain specific stuff. As Swift's Integers are built to prioritize performance and are fairly "close to the metal," this sort of problem is what you'd expect.
If you want to create beautiful edifices of pure mathematics, go write in Haskell. That's not what Swift or its standard library were built for.
Why not?
> As Swift's Integers are built to prioritize performance and are fairly "close to the metal," this sort of problem is what you'd expect.
I don't understand how one follows from the other. This is stuff that should be expanded at compilation stage, there's no runtime overhead. Likely that whole function should be specialised for each usage and be small enough to inline it. Since the loop has a known number of iterations, I even wouldn't be completely surprised if it was just a few basic instructions with array check skipped in some cases.
Computation is power, which is always going to be at a premium on mobiles in a competitive market. But if you want to have beautiful edifices of mathematics in Swift, you can always use Swift to write your own math library, and still get better performance than cpython. Expecting Swift's built in types to trivially be your math library is wishing the design goals were different.
I look at this and think "why would you want to write generic code for all those ints?"
The integer types may look similar but they're different in more ways than they're similar. They have different bit sizes, different signedness. The CPU literally has to do different things depending if its `uint8` or `int64`. So why do you want or expect one piece of code that does it all?
It's just so much easier and faster to do it like Go, have non-generic functions that do exactly what you want and as a result, get meaningful work done. It's faster to write (because you don't need to figure out how to do in a generic way), faster and easier to read, and possible to make changes to one func but not others.