This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.
  
    I propose an addition to, or library for, Ruby to allow a form of type checking that I am referring to as "interface checking."
    
    Since everything in Ruby is an object, it's less important that an object be of a certain type than that it implements a certain interface. After all, we don't care what an object is, we just care that it can do what we need it to do.
    
    An "interface" in my usage below is just a name, an :id tag, that is assigned to objects when they wish to declare "I implement this interface."
    
    A class that wishes to declare that it implements an interface can do so in several ways. Some examples:
    
    
  class MyClass
  end
The above class implicity declares "I implement the MyClass interface." Pretty simple.
    
    Now another:
    
    
  class MyClass < BaseClass
  end
The above class inherits from BaseClass, and so declares "I implement both the BaseClass and MyClass interface."
    
    Here's another:
    
    
  class MyClass
    include BaseClass
  end
Now MyClass mixes-in BaseClass, so it again declares "I implement both the BaseClass and MyClass interface."
    
    Now, here's an unusual example:
    
    
  class MyClass
    interface BaseClass
  end
The above class declares "I implement both the BaseClass and MyClass interface" but it DOES NOT inherit from BaseClass. In this case, "interface" is a new keyword used to make the declaration without adding any functionality to the class. The declaration is just that: a declaration. It does not require inheritance or mixing-in a module. It's merely a declaration.
    
    Every interface an object implements (declares to) could be stored as an array of :id values, much like the ancestry is stored. Every object could have a new method #implements?(:id) so we could ask if an object implements a certain interface.
    
    A simple form of parameter checking could also be introduced to Ruby, as (optional) syntactic sugar, based on these interface declarations. An example:
    
    
  def mymethod(:interface_id param)
  end
The above method requires that its only parameter "param" implement the interface identified by :interface_id. If the programmer attempted to pass an object which did not declare itself as implementing this interface, Ruby could throw an informative error for the developer to handle.
    
    So, in summary, I propose:
    
    1) Adding an interface chain, similar to the ancestors chain, to every object, which an object can add to in order to declare that it implements an interface.
    
    2) Automatically adding to an object's inheritance chain: its own name, the name of all inherited classes and all mixed-in classes.
    
    3) Adding an interface keyword so objects can make that declaration without having to inherit or actually implement the interface.
    
    4) Adding (optional) syntactic sugar to method parameters to perform enforcement that objects passed to a method declare themselves as implementing a given interface.
    
    5) Adding an #implements? method to all objects to perform dynamic queries on an object's interface chain.
    
    I believe this would an appropriate form of type checking for Ruby which is highly flexible, and completely ignorable if you wish. I think it win over more developers to Ruby who are skeptical about having zero type checking facilities while preserving Ruby's dynamic typing nature.
    
    I also think it would help people integrate into their projects, outside code from libraries and code generated by other developers on multi-developer projects by giving them more useful error messages when they pass the wrong objects to methods which are not, or poorly, documented.
    
  
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog