What static typing makes difficult
james anderson
james.anderson at setf.de
Sat Oct 25 18:49:08 EDT 2003
"Brian McNamara!" wrote:
>
> james.anderson at setf.de once said:
> ...
> >
> >? would haskell suggest to handle the attribute-based specialization in a
> >manner similar to the original post, or does it offer some other mechanism?
>
> There are two ways to go. If it's reasonable to know statically about
> this attribute, then you could encode it in the type system (e.g. a type
> like DOMWithDocumentElement). My hunch (based on my very limited
> knowledge of the domain) is that this is a dynamic attribute. So
> instead you'd 'fail' if the attribute didn't match at run-time. Note
> that my Haskell code returned "Maybe XMLRep"s, so the corresponding code
> would look something like
>
> -- has type Maybe XMLRep
> if (hasAttr domObj "documentElement")
> then Just ... -- do the conversion of the domObj
> else Nothing
>
> The same strategy would be used to cope with other 'dynamic failures'
> in the other branches, such as a malformed XML string, or a
> non-existent file when converting from a filename.
one favor which clos brings to the party is a fairly direct means to express
type constraints with arbitrary valence. generic functions turn the original
__init__ sort of "inside-out" and suggest an expression more like:
(defclass XML_Objectify () ())
(defgeneric __init__ (what from)
(:method ((instance XML_Objectify) (source string))
(case (char source 0)
(#\< (with-input-from-string (stream source)
(__init__ instance stream)))
(t (__init__ instance (intern-uri source)))))
(:method ((instance XML_Objectify) (source http-uri))
(with-open-uri (stream source :method :get)
(__init__ instance stream)))
(:method ((instance XML_Objectify) (source pathname))
(with-open-file (stream source :direction :input)
(__init__ instance stream)))
(:method ((instance XML_Objectify) (source stream))
(__init__ instance (parse-document source)))
(:method ((instance XML_Objectify) (source xml-document))
(dolist (element (children (root source)))
(setf (slot-value instance (name-symbol (name element)))
(read-from-string (value element))))))
if one needed additional specialization, one could add additional parameters,
as in
(defgeneric __init__ (what from how) ...)
from some perspectives this is "easy". from others perhaps not.
...
More information about the Python-list
mailing list