Updated 2012-01-30 11:26:42 by RFox

I tried to find a way to simulate inheritance in snit. I've found a way using snit macros

Does anybody have more ideas on this ?

The drawback is we could not add extra specific code in typeconstructor of Kangaroo Type (typeconstructor is already defined in ::Animal_inherit_macro !) ...

This is a short example :
  package require snit
  
  proc snitDefineInheritMacro {snitType} {
    set component [string tolower $snitType]_component  
    uplevel "
      snit::macro ::${snitType}_inherit_macro \{\} \{
        typevariable ${snitType}_install_component
        component ${component} -inherit true
        delegate option * to ${component}
        delegate method * to ${component}
        typeconstructor \{
          set ${snitType}_install_component \{install ${component} using $snitType %AUTO%\}
        \}
        destructor \{
         catch \{${component} destroy\}
        \}
      \} 
    "
  }
  
  proc snitCheckMandatoryOptions {args mandatoryOptions} {
    array set A $args
    foreach op $mandatoryOptions {
      if {![info exists A($op)]} {
        error "Mandatory option $op"
      }
    }
  }
  
  
  ::snit::type Animal {
     option -name
     option -species
     option -weigth  "undefined"
     option -life_time "undefined"
     constructor {args} {
       $self configurelist $args
     }
     typemethod get:mandatoryOptions {} {
       return "-species -name"
     }
     method get:info {} {
       puts "I'm a $options(-species). My name is $options(-name)"
     }
  }
  
  # Define snit macro for Types that will "inherit" from Animal
  snitDefineInheritMacro Animal
  
  #Kangaroo "inherit" from Animal
  #In fact , kangaroo objects have a Animal component 
  #witch is use to delagate all Animal options/methods to ...
  ::snit::type Kangaroo {
     option -jump_length
  
     #Call macro generated by : 'snitDefineInheritMacro Animal' line
     ::Animal_inherit_macro
  
     constructor {args} {
        #install Animal component : $animal_component
        eval $Animal_install_component
  
        $self configurelist $args
        #Control mandatory options
        snitCheckMandatoryOptions $args [concat [Animal get:mandatoryOptions] \
                                                [list -jump_length]]
     }
     method get:info {} {
       puts "[$animal_component get:info] I'm jumping  $options(-jump_length) feets !"
     }
  }

Now we can build jumpy kangoo object :

%>Kangaroo %AUTO% -jump_length 100 -name "jumpy kangoo" -species "mammiferes"

::Kangaroo1

%> ::Kangaroo1 cget -species

mammiferes

%>::Kangaroo1 cget -jump_length

100

%>::Kangaroo1 get:info

I'm a mammiferes. My name is jumpy kangoo I'm jumping 100 feets !

Is there a SNIT guru to confront this to what is explain in snitfaq overview? [1]: Note that you can achieve the effect of inheritance using COMPONENTS and DELEGATION--and you can inherit from anything that looks like a Tcl object.

I mean: Can a SNIT guru give us the code to achieve the effect of inheritance as explain in snitfaq using same example of this kangaroo?

RFox Well I think adding inheritance to Snit sort of makes it not Snit myself. Snit is intentionally a component/composition based system.