[Nitro] Annotation

transfire at gmail.com transfire at gmail.com
Mon Jan 29 12:09:41 EST 2007



On Jan 29, 10:45 am, "George Moschovitis" 
<george.moschovi... at gmail.com> wrote:
> This is what I use in Nitro now:
>
> T.ann!(:self)[:list] ||= []
> T.ann!(:self)[:list] << 1
>
> are the ! chars needed?

i think there is no way around it. and in fact i slightly mispoke in 
my other post. when assigning values to a key the ! causes the 
heritage to be copied to the current class.

  def ann!( ref, keys_or_class=nil, keys=nil )
    return annotations[ref] unless keys_or_class or keys

    if Class === keys_or_class
      keys ||= {}
      keys[:class] = keys_or_class
    else
      keys = keys_or_class
    end

    if Hash === keys
      ref = ref.to_sym
      annotations[ref] ||= {}
      annotations[ref].update(keys.rekey)
    else
      key = keys.to_sym
      annotations[ref][key] = heritage(ref)[key].dup  # HERE
    end
  end

If it did not do this then when you did:

   T.ann!(:self)[:list] << 1

It would effect the superclass of T if the annotation were defined 
there. Example:

  class Q
    ann :self, :list => []
  end

  class T < Q
  end

  T.ann(:self, :list) << 1
  Q.ann(:self, :list)  #=> [1]
  T.ann(:self, :list)  #=> [1]

but

  T.ann!(:self, :list) << 1
  Q.ann(:self, :list)  #=> []
  T.ann(:self, :list)  #=> [1]

The ! effectively cuts T off from Q.

Hmmm.... I think we need to take a moment to discuss annotations in 
general. Should I start a thread or do you wnat to go IRC?

T.



More information about the Nitro-general mailing list