Hi!

To allow a better documentation of classes, I added a "var" method to
the kernel module which helps me to write down instance variable
definition is a readable manner.  I'd like to some opinions whether
this is a stupid idea or a useful one.

Here's an example how to use it:

class Game
  var :name,      'String'
  var :turn,      'Integer'
  var :players,   'Array<Player>', :readonly
  var :worlds,    'Array<World>', :readonly
end

class Player
  var :name,      'String'
  var :email,     'String'
  var :resources, 'Integer'
end

And here's the implementation of "var":

module Kernel
  def var(name, type='Object', *access)
    attr_reader name if access.empty? or access.include? :readonly
    attr_writer name if access.empty? or access.include? :writeonly
  end
end

An interesting variant is to incorporate runtime type checking:

module Kernel
  def var(name, type='Object', *access)
    attr_reader name if access.empty? or access.include? :readonly
    if access.empty? or access.include? :writeonly
      eval "def #{name}=(obj)
        assertType(#{type =~ /\w+/; $&)}, obj)
        @#{name}=obj
      end"
    end
  end

  private

  def assertType(type, object)
    raise "was #{object.class} but should be #{type}" \
	unless object.kind_of? type
  end
end

This currently only works for non-generic types, but with more effort
(and meta-magic) it should be possible to also support Array<E> and
Hash<K,V> style type checks.  My idea is to wrapp arrays and hashes
when they are assigned to the instance variable using some proxy
object which then intercepts the []= method and delegates all other
methods to the original.


bye
-- 
Stefan Matthias Aust \/ Truth Until Paradox