From: Michael Neumann Date: 2001-05-15T19:25:10+09:00 Subject: [ruby-talk:15151] Re: Discussion on new Ruby features Christian Szegedy wrote: > On the other hand, if you think that your code is unsafe, > since some variable x should always be of class X, then, > given some simple syntax, it would be easier (shorter code, > and probably more effective, since it could be built in into > the interpreter) than redifining the writer function of x, > and programm the check in Ruby. do you think of something like: def addInteger(a : Fixnum, b : Fixnum) # ... end or perhaps with additional range checking: def addInteger(a :< Fixnum, b > 0 : Fixnum) end which replaces: def addInteger(a, b) raise "Wrong type" unless a.is_a? Fixnum and b.is_a? Fixnum # ... end Of course sometimes it would be nice to check for types this way, but it's not often enough used, I guess. For that you could befine a method "must" in Object (like the one in amstd), which can be used this way: a.must Fixnum And raises an exception if the type is not equal. > I think that three type of restrictions could be interesting: > > 1) Very Strict: variable x is exactly of class X > (no inheritance allowed) > 2) Strict: variable x of some class derived from class X > 3) Dynamic (like the signature mechanism in gcc): > variable x allows all functions of a module. > (this would allow the compiler to create some signature > table and pass it instead of the variable.) The problem is that classes in Ruby are just objects, i.e. they are dynamic. They have no static signature to check for. What you could do is define a interface definition, and check if the object "implements" this interface. But this could be done in pure Ruby, I think. Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de