[Haskell-cafe] using Typeable with STRefs
Antoine Latter
aslatter at gmail.com
Mon Mar 16 22:02:18 EDT 2009
On Mon, Mar 16, 2009 at 8:08 PM, Michael Vanier <mvanier42 at gmail.com> wrote:
> Ryan,
>
> So, if I understand you correctly, my only option is to use an IORef instead
> of an STRef? What I'm trying to do is implement a mutable box type as part
> of a dynamically-typed language I'm implementing in Haskell (which is mainly
> an exercise to improve my Haskell programming; mission accomplished). It
> bothers me that I have to use an IORef for this, since I don't see what this
> has to do with I/O. Similarly, if I wanted to have a mutable array type, I
> couldn't use STArray; I'd have to use IOArray. Or, I suppose I could define
> a richer Value type that had extra constructors for stateful types.
>
Why not:
data Value = forall a . Typeable a => V a
type STValue s = STRef Value
Then you have a dynamic, mutable box.
Also, even if the language you're interpretting is dynamicaly typed,
it doesn't mean that you need to use Haskell Dynamics. It should be
enough to do something like:
data Value
= VInt Integer
| VStr String
| VChar Char
and then:
-- | Similar to Prelude.Head
primOpHead :: Value -> Value
primOpHead (VStr xs) = VChar $ head xs
primOpHead _ = error "Type Mismatch!"
I'm sure other people can point you to much better ways to do this -
the point is that you don't need Dynamics to implement a dynamic
language.
Antoine
More information about the Haskell-Cafe
mailing list