Performance-loss of accessing data through class?

Peter Simons simons at cryp.to
Sat Oct 9 21:00:54 EDT 2004


Hi,

in a module I am writing, I am using a 'StateT st IO' monad
with a state like this:

  data MyState st = ST !Int !st

My own monad is yet-another wrapper for ... another state
monad. And that's getting inconvenient.

So I wondered whether it would be good to define a class
that unified all those StateTs into _one_ state, like:

> class KnowsMyStuff a where
>   foo :: a -> Int
>   bar :: a -> Float
>   etc :: a -> [String]

Then I could write my functions so that they'd work on any
MonadIO which has some way of getting those instances
defined.

If I did that, how much performance would I lose? Accessing
those values would probably require one more level of
indirection, that can't be as fast as having a specific data
type, right?

Or is there some optimizer magic at work here?

Peter


P. S.: Right now the code is _very_ fast, and I'd rather
have that than an convenient interface, that's why I ask.



More information about the Glasgow-haskell-users mailing list