I’m trying to teach myself Haskell….I’ve
spent a few hours going through a few tutorials….and I sort of get the
basics…
My interest in Haskell is specifically around the strength
of the type system.
After many years of OOP though my brain is wired up to
construct software in that ‘pattern’….a problem for me at the
moment is I cannot see how to construct programs in an OO style in Haskell….I
know this is probably not the way to approach it…but I feel I need to
master the syntax before the paradigm.
I’m not fussed about mutability, and I’m not
fussed about implementation inheritance (as apposed to subtyping)…. my
concerns are encapsulation of ‘state’ and abstraction, and
extension/expansion….a simple example would be.
interface IShape
{
Int GetArea();
}
class Square : IShape
{
Readonly int length;
Public Square(int length) { this.length =
length; }
Int GetArea() { return length * length;}
}
Class Rectangle : IShape
{
Readonly int lengthA;
Readonly int lengthB;
Public Rectangle (int lengthA,int lengthB)
{ this.lengthA = lengthA; this.lengthB = lengthB; }
Int GetArea() { return lengthA * lengthB;}
}
Class Circle : IShape
{
Readonly int radius;
Public Circle(int radius) { this.radius =
radius; }
Int GetArea() { return pi * radius * radius;}
}
Client code…..
Void Foo(IShape shape)
{
// look!....I know nothing except its of type IShape.
Int area = shape.GetArea();
}
I can obviously at a later date add a new class Triangle,
and not have to touch any of the above code….