TIP 567: Add Operation to Support Set-like Slots

    Author:         Donal K. Fellows <[email protected]>
    State:          Final
    Type:           Project
    Vote:           Done
    Vote-Summary:   4/0/2
    Votes-For:      DF, JN, MC, KW
    Votes-Against:  none
    Votes-Present:  DP,SL
    Created:        22-Feb-2020
    Tcl-Version:    8.7
    Keywords:       Tcl, TclOO, slots
    Tcl-Branch:     tip-567


This TIP adds an operation to slots to make it easier to manage slots that have set-like semantics.


Some slots are very much like slots, in that repeated entries are forbidden. Examples of this are the superclass slot of classes, and the mixins slots of classes and instances. This matters because there is sometimes a need to ensure that a particular value is in a slot without basing the operation on whether the value is already there. It's possible, of course, to do this with the existing operations, but the code to do so is moderately complex due to the need to handle resolution of the class names; it's simpler to add the operation to the slot itself.

This TIP fixes a subtle bug in the implementation of TIP #500 found during the development of the implementation of TIP #560.


The following method will be added to the class ::oo::Slot:

The slots containing classes (specifically superclass and mixin, the latter on both classes and instances) additionally gain the requirement that their elements be unique within the slot.


The core of the implementation is simply this:

method -appendifnew -export args {
    set my [namespace which my]
    set current [uplevel 1 [list $my Get]]
    foreach a $args {
        set a [uplevel 1 [list $my Resolve $a]]
        if {$a ni $current} {
            lappend current $a
    tailcall my Set $current

The full implementation (including a number of bugfixes enabled by this) is on the tip-567 branch.


This document is placed in public domain.