I\'m pretty new a Haskell programming. I\'m trying to deal with its classes, data, instances and newtype. Here is what I\'ve understood:
data NewData = Const
To make sum types like
data NewData = Constr1 Int Int | Constr2 String Float
I usually do the following in c#
interface INewDataVisitor {
R Constr1(Constr1 constructor);
R Constr2(Constr2 constructor);
}
interface INewData {
R Accept(INewDataVisitor visitor);
}
class Constr1 : INewData {
private readonly int _a;
private readonly int _b;
Constr1(int a, int b) {
_a = a;
_b = b;
}
int a {get {return _a;} }
int b {get {return _b;} }
R Accept(INewDataVisitor visitor) {
return visitor.Constr1(this);
}
}
class Constr2 : INewData {
private readonly string _a;
private readonly float _b;
Constr2(string a, float b) {
_a = a;
_b = b;
}
string a {get {return _a;} }
float b {get {return _b;} }
R Accept(INewDataVisitor visitor) {
return visitor.Constr2(this);
}
}
This isn't quite the same in terms of type safety because an INewData can also be null, might never call a method on the visitor and just return default(R), might call the visitor multiple times, or any other silly thing.
A c# interface like
interface SomeInterface {
public bool method1(List someParam);
}
Is really more like the following in Haskell:
data SomeInterface t = SomeInterface {
method1 :: [t] -> bool
}