What static typing makes difficult

Remi Vanicat vanicat+invalid at labri.fr
Sat Oct 25 17:03:58 EDT 2003


gt5163b at prism.gatech.edu (Brian McNamara!) writes:

> 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.

By the way, this system have one big advantage with regard to the
David Mertz example : Every one can had a new type to the
ConvertibleToXML class, when with David example, one have to change
the class XML_Objectify to have the same effect.

-- 
Rémi Vanicat




More information about the Python-list mailing list