What is a type error?

Chris Smith cdsmith at twu.net
Thu Jun 22 10:09:32 EDT 2006


Pascal Costanza <pc at p-cos.net> wrote:
> What about this: You get a type error when the program attempts to 
> invoke an operation on values that are not appropriate for this operation.
> 
> Examples: adding numbers to strings; determining the string-length of a 
> number; applying a function on the wrong number of parameters; applying 
> a non-function; accessing an array with out-of-bound indexes; etc.

Hmm.  I'm afraid I'm going to be picky here.  I think you need to 
clarify what is meant by "appropriate".  If you mean "the operation will 
not complete successfully" as I suspect you do, then we're closer... but 
this little snippet of Java (HORRIBLE, DO NOT USE!) confuses the matter 
for me:

    int i = 0;

    try
    {
        while (true) process(myArray[i++]);
    }
    catch (IndexOutOfBoundsException e) { }

That's an array index from out of bounds that not only fails to be a 
type error, but also fails to be an error at all!  (Don't get confused 
by Java's having a static type system for other purposes... we are 
looking at array indexing here, which Java checks dynamically.  I would 
have used a dynamically typed language, if I could have written this as 
quickly.)

I'm also unsure how your definition above would apply to languages that 
do normal order evaluation, in which (at least in my limited brain) it's 
nearly impossible to break down a program into sequences of operations 
on actual values.  I suppose, though, that they do eventually happen 
with primitives at the leaves of the derivation tree, so the definition 
would still apply.

-- 
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation



More information about the Python-list mailing list