[Haskell-cafe] OOP'er with (hopefully) trivial questions.....

Nicholls, Mark Nicholls.Mark at mtvne.com
Mon Dec 17 05:46:23 EST 2007


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.... 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071217/f4410e08/attachment.htm


More information about the Haskell-Cafe mailing list