[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: From dynamic to static
- From: David Given <dg@...>
- Date: Mon, 31 Oct 2005 18:19:05 +0000
On Monday 31 October 2005 17:06, PA wrote:
> http://www.mindview.net/WebLog/log-0025
He wants to use OCaml, he does.
OCaml is a strong, polymorphic, statically typed language where, ideally, the
compiler guesses what types you're using automatically. If you say:
let fnord x = x
...then it knows that the function fnord will work for all xes, regardless
what type they are. If you say:
let fnord x = x + 1
...then it knows that x must be an integer (because + only works on integers).
But the really cunning bit is that this applies to objects too. I can't
remember the syntax, so I'll switch to Lua-esque pseudocode. This:
o:method1()
...tells the compiler that o must be an object that implements method1. This
means that you can pass in *any object* that happens to implement method1 ---
it'll work, and be statically typed. Using his example, you could implement
Cat, Dog and Bob has three seperate classes, and provided they all
implemented the 'speak()' method, this function:
function command(pet)
pet:speak()
end
...would work on all of them *and be type-safe*. If you like, the compiler has
generated an implicit interface containing the 'speak' method.
It's very, very neat, and manages to give you the best of both worlds; strong,
statically checked types, and easy-to-write, polymorphic code.
Unfortunately, OCaml doesn't quite live up to this. There's a whole bunch of
issues with the language design which means you have to help the type checker
along, and it's very hard to produce 'nil' object pointers --- when I last
tried to write a real program in it, I eventually gave up in disgust.
I don't see any reason why you couldn't implement a Lua type checker that used
the same concepts. It would have to analyse a whole program at once, of
course, and some of Lua's more dynamic features would manage to bypass the
type checker... but it should be possible to define a subset of the Lua
semantics that could be usefully typechecked, but would also run on a
standard interpreter (which means no explicit type annotations).
Unfortunately, I suspect this is beyond my skills...
--
+- David Given --McQ-+ "If God spammed rasfw with the answer to Life, the
| dg@cowlark.com | Universe, and Everything, I'd report him to
| (dg@tao-group.com) | abuse@heaven.org." --- David Bilek
+- www.cowlark.com --+
Attachment:
pgpv5asc69GbN.pgp
Description: PGP signature