[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Language War (Re: "C" Manual)



Lo, on Sunday, January 13, Erik Steffl did write:

>   type is a propert of variable.

Not exclusively.  Two counter-examples, one in C, and one in Scheme.

C:
    int x;

    x = "foo";

You'll get a type error here at compile time, for obvious reasons.
Question: how can this be a type error if only variables have types?
You need to realize that "foo" has type (const) char * before you can
determine that you can't assign it to an int.

Scheme:

(define f
  (lambda (x)
    (cond
      ((boolean? x) (if x 42 23))
      ((symbol? x) (string-length (symbol->string x)))
      ((char? x) (char->integer x))
      ((vector? x) (vector-length x))
      ((procedure? x) (x 42))
      ((list? x) (length x))
      ((pair? x) (car x))
      ((number? x) (- x))
      ((string? x) (string-length x))
      ((port? x) (read x))
      ((promise? x) (force x)))))

This defines a function f with one argument, x.  What's x's type?  The
function is equally well-defined for an argument of just about any value
supported by R5RS, the current spec.  (For that matter, what's the
return type of f?  Answer: it's usually a number, but it depends on x!)

In languages like Scheme, Lisp, Python, and Smalltalk, almost all
typechecking is deferred to run-time.  Therefore, it is meaningless to
describe the type of a variable; in these languages, types only apply to
values.

Richard



Reply to: