I am trying to write a larger piece of code using only type constraints for 
all the numbers, not specific types, in order to be able to choose the 
precision as late as possible. This works rather well (something I can't say 
of many other languages), but one problem I keep running into is constants.

Something I need frequently is, for example, the Boltzman constant, 
0.0083144708636327096 in my unit system. I can certainly type 
0.0083144708636327096 everywhere in the code and make things work, literals 
have the nice property of being overloaded. But for the sake of readibility, 
I prefer to give this beast a name and have the explicit value only once in 
my code. So I create a module "Constants" with something like

k_B = 0.0083144708636327096

The trouble is that k_B then becomes "Double" by default (or any other type I 
declare it to be). And this ruins all the code where I make a reference to 
it, with Hugs telling me "Inferred type is not general enough" because all my 
"generic" types become unified with Double.

The only solution I can see is to write a function "fromDouble" in analogy to 
"fromInteger" and put this in front of all named constants. But how could I 
implement such a function?

I might also declare all my constants to be "Rational" and use "fromRational", 
but I don't know much about the "Rational" type. Do I have to worry about 
insufficient or compiler-dependent precision?

Konrad.


_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to