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 -speciesmammiferes%>::Kangaroo1 cget -jump_length100%>::Kangaroo1 get:infoI'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.

