Version 1 of Using itcl init code fragment

Updated 2002-11-27 15:59:10

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.

  #
  # tcl 8.3.4 / itcl3.2 / on windows
  # 
  # 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 { [itcl_info 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"