[Haskell-cafe] Trapped by the Monads

Mark Carter mcturra2000 at yahoo.co.uk
Tue Sep 20 16:03:26 EDT 2005


Bill Wood wrote:

> Could you briefly elaborate on what you mean by "hybrid variables"?
>  
>
>  
>
According to Google, hybrid in genetics means "The offspring of 
genetically dissimilar parents or stock, especially the offspring 
produced by breeding plants or animals of different varieties, species, 
or races." It's kind of like that - but for variables.

The typical example in C is:
 mem = malloc(1024)
Malloc returns 0 to indicate that memory cannot be allocated, or a 
memory address if it can. The variable mem is a so-called hybrid 
variable; it crunches together 2 different concepts: a boolean value 
(could I allocate memory?) and an address value (what is the address 
where I can find my allocated memory).

It's considered a bad idea because it makes it easy for programmers to 
use the value inappropriately - witness the number of programmers who 
pass in 0 as a memory location. The suggested solution is to give each 
variable one meaning. So, I could define, say, a variable called 
success, and do something like
mem =improved_ malloc(1024, & success)
if(success)
{
    do_something(mem)
}
else
{
    oops()
}

Admittedly, there's nothing to force the programmer to actually check 
the success variable; but at least the idea is that everything is much 
more explicit, and the programmer is less likely to shoot himself in the 
foot.


	
	
		
___________________________________________________________ 
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com


More information about the Haskell-Cafe mailing list