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

Re: Dylan type-safety




Dylan is dynamically typed.  With the specializers on variables,
though, the programmer may give more type information to the compiler.
What the compiler chooses to do with that information is somewhat
open-ended.  To simplify a little, suppose there is some code such as

  ...
  let a :: <foo> = b;
  ...

A simple, purely dynamic, implementation would look up the value for
the variable b, check that instance?(b, <foo>), and, if so, assign
that value to a.  All type errors would be detected at run time.

A more sophisticated approach would be to analyze the code to try to
figure out the types of all values that flow into b.  Possibly (and
especially if the programmer makes good use of type annotation) the
compiler may figure out that all possible values of b will be of type
<foo>.  For example, maybe the preceding line is

   define method m (b :: <foo>)

In this case, the compiler may omit the type check before the
assignment.   Alternatively, the compiler may deduce that all possible
values of b will _not_ be an instance of <foo>;  in which case a
compile time error would be signalled.  Finally of course, the
compiler may not be able to tell, and some sort of dynamic type check
will be necessary.

One more thing that the compiler could do with the results of its
analysis is to tell the programmer what it deduced.  If the compiler
indicates all points where either a type check or a possibly-failing
dynamic dispatch occur, then code which has none of these points could
legitimately be called "statically type correct". 

Harlequin Dylan has a similar feature -- code coloring (see View/Color
Dispatch Optimizations) in the HD Editor.  This technically colors
code according to optimizations applied to call sites.  Since code
cannot be directly called or inlined if there is a possibility of
dispatch failure, an "optimized" call site can be viewed as type
correct.

>>>>> "Harry" == Harry Chomsky <harryc@chomsky.net> writes:

 Harry> A discussion has come up in some other newsgroups (comp.lang.object a few
 Harry> weeks ago, comp.lang.eiffel yesterday) about whether Dylan has strong static
 Harry> type-checking.  My impression, as a relative newbie, is that it doesn't:
 Harry> even if you scrupulously declare the types of all your variables and
 Harry> methods, you can easily write code that causes run-time type errors with no
 Harry> warning from the compiler, and with no indication in the code that you're
 Harry> doing anything dangerous.

-- 
Greg      gregs@ai.mit.edu (617)253-5807
Sullivan  http://www.ai.mit.edu/~gregs/




Follow-Ups: