Subject: ANN: majorminor.tcl - add subcommands easily in pure Tcl - DN [1]


Alexandre Ferrieux <alexandre.ferrieux@cnet.francetelecom.fr> - 05 May 1999 - comp.lang.tcl

 Here: http://place.net/~af/tcl/majorminor.tcl is a sample pure-Tcl
 implementation of a new command lookup mechanism ('major/minor')
 described perviously at
 http://www.dejanews.com/[ST_rn=ps]/getdoc.xp?AN=472294276.

 See the description below.

  Like for the previous 'curry' package, I am posting it here at a very
 early stage both for feedback and as a potential incentive for
 core-patchers to reimplement it with the best performance. In the
 meantime, this simple standalone sourceable script will let people
 validate/invalidate the semantics.

 # Principle:

 # This  package   aims   at  easing  the  process  of  overriding  tcl
 # subcommands.  It does so by  silently replacing  overridden commands
 # by  a  wrapper  that  tries to match arguments against  the prefixes
 # given by the programmer as special cases (cmd  subcmd subsubcmd...).
 # These prefixes  are scanned by decreasing length, so that in case of
 # conflict the most specific wins:

 #    % proc foo {x y} {puts "Generic foo: $x $y"}
 #    % subproc "foo bar baz" {} {puts AAA}
 #    % subproc "foo bar" z {puts BBB}
 #    % foo 3 4
 #    Generic foo: 3 4
 #    % foo bar 2
 #    BBB
 #    % foo bar baz
 #    AAA

 # Performance  issue:  of  course, the  wrapper  adds overhead to  the
 # non-overridden  case. Worse, if you override "string compare" you'll
 # disable the inlining of [string  ...] by the byte  compiler,  so the
 # overall effect will be awful. However:
 #        - in most cases (non-inlined procedures) this overhead will be
 #          bearable.
 #        - this  script is provided only as a proof of concept. The aim
 #          is  of course  a core  patch (once  the semantics  is agreed
 #          upon).

 # As it is often witnessed in overriding frameworks (like OO), it is
 # often handy to be  able to call the  unmodified "inherited" behavior
 # within you overriding procedure. To do this, the [inherited] call is
 # provided:

 #    % subproc "string compare" {x y} {
 #        puts stderr "I've been here !"
 #        inherited string compare $x $y
 #      }

 # More generally, in a [subproc  "a b c d e" ...] one is expected to
 # to call [inherited "a b c d" e ...] to avoid unwanted recursion.

 -Alex

Last modified
1999-09-27

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated