3 ms·
"This is a fairly unique and cool aspect of Haskell, especially if you come from the C++ world". That's not true at all - what Haskell has here that C++ or D d
by atilaneves 9y ago
"This is a fairly unique and cool aspect of Haskell, especially if you come from the C++ world".
That's not true at all - what Haskell has here that C++ or D don't is type inference so that explicit type annotations aren't needed. The read function in C++ would be
#include <string>
#include <iostream>
using namespace std;
template<typename T>
T read(const string& str);
template<>
int read(const string& str) {
return stoi(str);
}
template<>
double read(const string& str) {
return stod(str);
}
template<>
string read(const string& str) {
return str;
}
int main() {
cout << read<int>("2") << endl;
cout << read<double>("3.14") << endl;
cout << read<string>("foobar") << endl;
}
- danharaj 9y agoThe article doesn't cover this but haskell's return type polymorphism is strictly more powerful because it can be used in cases where you don't know the type at compile time. Such a situation can occur with recursion on nested data types or with higher rank types. Now, i bet with some hackery one could emulate this too, but I don't expect it to be legible.