What static typing makes difficult
Brian McNamara!
gt5163b at prism.gatech.edu
Sat Oct 25 16:35:19 EDT 2003
mertz at gnosis.cx once said:
>The package gnosis.xml.objectify takes an XML source, and turns it into
>a "native" Python object. The function make_instance() can accept a
>wide range of different things that might sensibly relate to XML: a DOM
>object, a filename, an XML string, any object with a .read() method.
>Just one function deals happily with whatever you throw at it--without
>any deep commitments about what type of thing it is (i.e. some novel
>file-like object, or some new DOM implementation work without any
>problem).
I have no chance at a full implementation, but here is a sketch in
Haskell. I know that a mere sketch is never as good as a working
implementation, so I hope someone else will take up the challenge.
Anyway:
type XMLRep = ... -- "internal" representation of XML objects
class ConvertibleToXML a where
convertToXML :: a -> Maybe XMLRep
instance ConvertibleToXML DomObject where
convertToXML :: DomObject -> Maybe XMLRep
convertToXML aDomObj = ...
instance ConvertibleToXML String where
convertToXML :: String -> Maybe XMLRep
convertToXML s = if (head s) = '<'
then XMLStringToXML s -- assume an XML string
else readXMLFromFileNamed s
-- yes, we'd need to be in the IO monad here
-- Later in the program
someFunc x y =
...
let xml = convertToXML x in ...
-- which will infer the constraint "ConvertibleToXML x"
As far as I can tell, the only "extra scaffolding" is the type class
ConvertibleToXML. Each time some new data type comes along which can be
converted to XML, we add a new instance declaration which shows how.
--
Brian M. McNamara lorgon at acm.org : I am a parsing fool!
** Reduce - Reuse - Recycle ** : (Where's my medication? ;) )
More information about the Python-list
mailing list