Version 1 of dict in snit

Updated 2004-09-20 17:25:48

package require snit

    snit::type Dict {
        variable dictv

        method append {key args} {
            variable dictv
            return [eval ::dict append dictv [list $key] $args]
        }

        method exists {args} {
            variable dictv
            return [eval ::dict exists [list $dictv] $args]
        }

        method filter {ftype args} {
            variable dictv
            return [eval ::dict [list $dictv $ftype] $args]
        }

        method for {kv body} {
            variable dictv
            return [uplevel 1 ::dict for $kv $dictv $body]
        }

        method get {args} {
            variable dictv
            return [eval ::dict get [list $dictv] $args]
        }

        method incr {key {incr 1}} {
            variable dictv
            return [::dict incr dictv $key $incr]
        }

        method keys {{glob *}} {
            variable dictv
            return [eval ::dict keys [list $dictv] $glob]
        }

        method lappend {key args} {
            variable dictv
            return [eval ::dict lappend dictv [list $key] $args]
        }

        method remove {args} {
            variable dictv
            return [::set dictv [eval ::dict remove [list $dictv] $args]]
        }

        method replace {args} {
            variable dictv
            return [::set dictv [eval ::dict replace [list $dictv] $args]]
        }

        method set {args} {
            variable dictv
            return [::set dictv [eval ::dict set dictv $args]]
        }

        method size {} {
            variable dictv
            return [::dict size $dictv]
        }

        method unset {args} {
            variable dictv
            return [eval ::dict unset dictv $args]
        }

        method values {{glob *}} {
            variable dictv
            return [::dict values $dictv $glob]
        }

        method subdict {sub args} {
            variable dictv
            return [$self set [eval ::dict create $args]]
        }

        method serialize {} {
            variable dictv
            return $dictv
        }

        method restore {ser} {
            variable dictv
            eval dict set dictv $ser
        }

        constructor {args} {
            variable dictv
            ::set dictv [eval ::dict create $args]
        }
        destructor {}
    }

WHD: Two questions: why are you declaring dictv in every method? You don't need to. Second, why are you explicitly qualifying "::set"? You don't need to do that either.