[Haskell-cafe] Re: [Haskell] Dynamic binding

Bulat Ziganshin bulatz at HotPOP.com
Fri Jun 24 03:35:12 EDT 2005

Hello Andrew,

Friday, June 24, 2005, 12:35:16 AM, you wrote:

AW> Assuming I have a list of shapes somewhere in my program. The list would
AW> be heterogeneous, or at least contain data constructed using various 
AW> different type constructors. The list would also be changing all the 
AW> time, for example in a drawing program. So, using the suggestion of a 
AW> list of drawing functions, would this not mean I would have to alter the 
AW> list of drawing functions each time I alter the list of shapes?

of course, we suggest this solution only for cases when all you need
from shapes - to draw them. as i write in previous letter, one time i
changed a list of regular expressions to list of functions that check
matches with these regular expressions because it was the only
operation i need

if you require several operations, you can pack them in tuple or
structure (really, tuple is a structure without field names)

if you need fields/interfaces inheritance, then this method will not
work. but in 90% of cases it's enough. in C++, creating classes is the
most useful method of structuring program, so it is used for any
problem - from very simple to most complex. when you translate such
thing to Haskell, it's better to see which concrete problem are solved
with help of classes, and not to emulate the classes itself (because
in this case you will write many unneeded code)

AW> It does 
AW> not seem an extensible solution. When it comes to adding new types or 
AW> new functionality for existing types would it not be easy to miss 
AW> updating a part of the program source that needed updating?

adding new "class" needs adding one "constructor" function. it's hard
to skip that part :)

adding new function to interface need to change interface structure
definition or adding new element to tuple. then type-checker of
Haskell compiler will force you to add new function to all places
where this structure/tuple are created. you just never worked with
such strong type-checking as in Haskell

AW> The example I have found the simplest is the one given by Lennart, 
AW> however this to me seems equivalent to the way people use switch 
AW> statements in C to mimic dynamic binding.

only because it's C-like :)  you just can't believe that Haskell
program can be 3-10 times smaller while keeping the same functionality :)))

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list