Mostly the approach taken is to either:
- Treat everything as a String, or
- Encode type information as metadata, either declaratively in the code, or externally (using some abomination such as WSDL).
I’ve never liked either of these approaches, as they seem to interfere with the spirit of a language such as Ruby or Smalltalk. So I got to thinking about Lazy typing.
In my own code, once I assign (say) a string to an instance variable, I’m fairly unlikely to then assign an array to it, or a regexp. Similarly, once an instance variable is a Fixnum, it tends to stay a Fixnum. So I’m wondering: could we do typing based on "first type in wins"? That is, once you assign an object to a variable, that’s it’s type.
Now I know that
| a: | this isn’t in any way original, and |
| b: | this won’t always work, but… |
…but the effort involved in coding it up was so small I just had to try it. The result is that you can say:
require 'LazyType'
class Dave
attr_accessor :var
end
d = Dave.new
d.var = "hello"
d.var = 123 # boom!
which produces:
in `var=': Bad type: got Fixnum, expected String (RuntimeError)
I’m just tossing this out for comment, but if anyone’s interested, the coffee-break implementation is here.




Comments