Note that `ref` stands for "reference", not "reference counting". The behavior 
will not differ between the reference counting and the mark and sweep GC.

You also do not strictly require `ref` for polymorphism to work, though this is 
the most common use case; any kind of pointer (`ref`, `ptr`, `var`, or 
pass-by-reference for value arguments) will work.

Example:
    
    
    type
      animal = object of RootObj
      dog = object of animal
      cat = object of animal
    
    method say(self: animal) = discard
    method say(self: dog) = echo "woof!"
    method say(self: cat) = echo "meow?"
    
    proc make_noise(a: var animal) =
      a.say; a.say; a.say
    
    proc main =
      var d: dog
      var c: cat
      d.make_noise
      c.make_noise
    
    main()
    

The reason why it doesn't work without pointers is that variables that aren't 
references (or somesuch) cannot themselves handle polymorphic types and will be 
coerced to the supertype upon assignment by hacking off any extraneous fields 
at the end of the subtype and changing the type field. Otherwise, it may be 
possible that method calls would try to access fields that do not exist in 
memory.

Reply via email to