Cant decide whether this list is appropriate for questions related to generics usage, or only to generics design? So let me know if I should use <a href="mailto:haskell@haskell.org">haskell@haskell.org</a> instead, but on the other hand I figure you guys are the experts :-)
<br><br>Trying to write an xmlserialization function for arbitrary (Data) objects, and not requiring a DTD or a code generation step.<br><br>It's possible in C# so it really ought to be possible in Haskell ;-)<br><br>
xml serialization is pretty easy, see section 9.1.1 of <a href="http://www.haskell.org/haskellwiki/HXT">http://www.haskell.org/haskellwiki/HXT</a><br><br>On the other hand deserialization is still a work in progress. At least I'm not smart enough to figure it out, and nothing comes up in Google.
<br><br>The xml parsing bit is done, see section 9.1.2 of <a href="http://www.haskell.org/haskellwiki/HXT">http://www.haskell.org/haskellwiki/HXT</a><br><br>So, we can get a list of pairs/triples etc containing the data of our choice, such as field names, field values (in string format), data types, constructors, etc.
<br><br>What's remaining is to take makeConstrM, our final data type, and the list of field values, and to create the final object.<br><br>Here's my non-working attempt at this bit so far. Most of the working bits come from a demonstration by kpreid in irc yesterday night of using makeConstrM with a pair of strings. The rest of the code is my feeble attempt to get this working for the Config custom data type.
<br><br>runM' :: (MonadState [String] m, Monad m, Data a) => m a
<br>runM' = do<br> value <- gets head<br> modify tail<br> -- then one of: (pick the non-working function of your choice ;-) :<br> -- return read (fromJust value)<br> -- return (fromJust $ cast value )<br>
-- return (fst $ head $ gread( "(" ++ value ++ ")" ) )<br> -- return (fromConstrM runM' constr)<br> -- return (fromConstr contr)<br><br>testConstrM' :: (Read a, Data a, Read c, Read b, Data b, Data c) => [String] -> a -> (b,c)
<br>testConstrM' fieldvalues object = evalState( fromConstrM runM' (toConstr object) ) fieldvalues<br><br>data Config = Config{ name :: String, age :: Int }<br> deriving( Data, Show, Typeable, Ord, Eq, Read )<br>
createConfig = Config "blah" 3<br><br>test = testConstrM' ["qsdfqsdf", "7"] createConfig<br><br>Note that whilst I'm a total Haskell newbie (this is week 2), I've used Reflection extensively in other languages, eg wrote a fast async rpc layer over guaranteed udp for C#
<a href="http://metaverse.svn.sourceforge.net/viewvc/metaverse/Trunk/Source/Metaverse.Networking/">http://metaverse.svn.sourceforge.net/viewvc/metaverse/Trunk/Source/Metaverse.Networking/</a> , so I know more or less what I'm aiming for, just not exactly how to do it ;-)
<br><br>