[q2x] Refactorization

Nick Trout nicktrout at shaw.ca
Mon Nov 10 12:38:25 EST 2003


>From: "Brian Hook" <hook_l at pyrogon.com>
> Python is dynamically typed as well as Lua, but I think Ruby is
> statically typed. I've programmed a lot more Python than Lua and
> I've never missed static typing. It doesn't affect scalability and
> its a less hassle.

> The problem is that you can't make a method that takes "two objects of
type X" and have the compiler catch this (at least, I believe you
can't do this in Lua).  Without this feature, it's entirely too easy
to accidentally pass parameters of the wrong type to a function.

You're right, but no dynamically typed language can as you obviously don't
know the type until runtime. You can of course check the type at runtime,
but I think problems like this are rare and can be fixed with documentation.

If we did want to do something like this you could wrap each function you
wanted to check, e.g.

function foo(a,b) -- 2 numbers
  return a*b
end

check_params('foo', 'number', 'number')  -- for debug

You don't really want to be doing type checking (efficiency issue), and
doing it this way you could just turn the functionality on for the debug
version. You might even generate this type wrappers automatically from the
docs.

>Where this becomes a problem is when you have projects with lots of
programmers and someone forgets/doesn't realize that you need to use
certain types in a particular method.

Docs! A simple comment with each function will fix this. I added a document
generator (selfdoc) to my GL project http://doris.sf.net/

> Don't get me wrong, I'm not a fan of hardcore static type checking,
but it is useful in some areas.  My preference is something like
Obj-C, which has both.

Haven't used Obj-C, I should look it up some time.

Nick





More information about the q2x mailing list