Using itcl init code fragment

How to use the Itcl "init code fragment" to pass args to the parent.

This allows to check arguments in the parent, before they are checked in the child class.

Not explicitly written in the documentation.

The point is that the parent constructor must be called prefixed by namespace::

Vincent Wartelle

  #
  # tcl 8.4.1 / itcl3.2 / tested on windows xp
  # 
  # how to parse args in parent before they are parsed in child
  # usage of init code fragment
  # 
  # constructor args ?init? body 

  foreach classid { parent child } {
      # avoid to delete already deleted classes
      if { [find classes $classid] != "" } {
          delete class $classid
      }
  }

  class parent { 
      public variable parent_a "";
      public variable parent_b "";    

      constructor { args } {

          puts stdout "parent this -$this- args -$args-"    
          eval configure $args
          # check value of parent_a
          if { $parent_a == "" } {
              puts "--> bad parent_a"
          }
      }
  }

  class child {
      inherit parent

      public variable child_a

      # pass arguments in an init code fragment        
      constructor { args } {eval "parent::constructor $args" } {
          puts stdout "child this -$this- args -$args-"
          eval configure $args
      }

  }


  child testobj -child_a "val_child_a" -parent_a "val_parent_a"

No, this should be implemented via itcl chain command.

David


See also incr Tcl