What is a type error?

Pascal Costanza pc at p-cos.net
Mon Jun 26 13:41:06 EDT 2006


David Hopwood wrote:
> Pascal Costanza wrote:
>> Chris Smith wrote:
>>
>>> While this effort to salvage the term "type error" in dynamic
>>> languages is interesting, I fear it will fail.  Either we'll all have
>>> to admit that "type" in the dynamic sense is a psychological concept
>>> with no precise technical definition (as was at least hinted by
>>> Anton's post earlier, whether intentionally or not) or someone is
>>> going to have to propose a technical meaning that makes sense,
>>> independently of what is meant by "type" in a static system.
>> 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.
> 
> This makes essentially all run-time errors (including assertion failures,
> etc.) "type errors". It is neither consistent with, nor any improvement
> on, the existing vaguely defined usage.

Nope. This is again a matter of getting the levels right.

Consider division by zero: appropriate arguments for division are 
numbers, including the zero. The dynamic type check will typically not 
check whether the second argument is zero, but will count on the fact 
that the processor will raise an exception one level deeper.

This is maybe better understandable in user-level code. Consider the 
following class definition:

class Person {
   String name;
   int age;

   void buyPorn() {
    if (< this.age 18) throw new AgeRestrictionException();
    ...
   }
}

The message p.buyPorn() is a perfectly valid message send and will pass 
both static and dynamic type tests (given p is of type Person in the 
static case). However, you will still get a runtime error.


Pascal

-- 
3rd European Lisp Workshop
July 3 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/



More information about the Python-list mailing list