Version 0 of snitInheritmacro

Updated 2007-08-08 09:45:31 by CyrilVartanian

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

Does anybody have more ideas on this ?

This is a short example :

<verbatim> 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 !"
   }

}

</verbatim>

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 !