21
Is it possible to emulate the type class functionality of Haskell with C++ (or C#) templates?
Does it make sense or is there any payoff in doing that?
I was trying to make a Functor class in C++ and I wasn't able. I tried something like this:
#include <iostream>
using namespace std;
//A function class to make types more readable
template <class input, class output> class Function {
private:
output (*ptrfunc )(input);
public:
Function(output (* ptr)(input)) {
ptrfunc = ptr;
}
output call(input x) {return (*ptrfunc)(x);}
output operator() (input x) { return call(x);}
};
//the functor "typeclass"
template <class a> class Functor{
public:
template <class b> Functor<b> fmap(Function<a,b> func);
};
// an container type to be declared "instance" of functor:
template <class a> class List : public Functor<a> {
private:
a * ptrList;
int size;
public:
List(int n) { //constructor;
ptrList = new a[n];
size = n;
}
List(List<a> const& other) { //copy constructor
size = other.size;
ptrList = new a[size];
for(int i = 0; i<size; i++)
(*this)[i] = other[i];
}
~List() { delete ptrList;} //destructor
a& operator[](int i) { return ptrList[i];} // subscript operator just for easy notation
const a& operator[](int i) const { return ptrList[i];}// subscript operator just for easy notation
template <class b> List<b> fmap(Function<a,b> func) { //"instance" version of fmap
List<b> temp(size);
for(int i = 0; i < size; i++)
temp[i] = func((*this)[i]);
return temp;
}
};
int test(int k) { return 2 * k;}
int main(void) {
Function<int, int> func(&test);
List<int> lista(10);
for(int i = 0; i < 10; i++)
lista[i] = i;
List<int> lista2(lista.fmap(func));
for(int i = 0; i < 10; i++)
cout << lista2[i] << " ";
cout <