[xquery-talk] XQuery treatment of xs:positiveInteger?

Jeni Tennison jeni at jenitennison.com
Tue Jul 20 18:21:20 PDT 2004


Hi Kent,

> Now let's see if I can't get Jeni mad at me: I have to disagree with
> the "be generous with what you accept."

Goody! I hoped I'd provoke some discussion :)

> Wouldn't good coding and security practices say the opposite? If the
> compiler complained about this (as I think it should,) then there's
> no need to cast and type correctness is more assured. Putting it
> another way, if I pass my:simple-add(-1,3), I'd rather have the
> compiler refuse to do the work rather than failing at run-time when
> it tried to cast -1 to positive integer.

I agree. I would like compilers to detect when values that are known
statically (e.g. given as literals) can't be cast to a particular type.

My rationale for saying that functions should be generous in what they
accept is simply that, being someone who writes code, I want writing
code to be easy. Since using types other than xs:string, xs:integer,
xs:decimal and xs:double often requires using an explicit cast, it is
easier to write the cast once, in the function definition, than to
write the cast in every function call. Where you put the cast
shouldn't make any difference to whether the compiler can catch it or
not.

Of course, in my ideal world, we wouldn't have to write these explicit
casts anyway. The arguments to functions would be specified to as
precise a type as they needed, providing better documentation of the
functions, supplying more precise error messages, and enabling more
helpful assistance from authoring tools. But we're pretty far from my
ideal world here, so I adjust my coding practice to match.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



More information about the talk mailing list