Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2009-07-29
19:41 • Ticket [745851ffff] dict merge status still Closed with 4 other changes artifact: 4be4d3048d user: dkf
2008-12-07
18:30 • Ticket [745851ffff]: 6 changes artifact: f46dc4dcc0 user: dkf
2004-03-13
06:33 • Ticket [745851ffff]: 6 changes artifact: ef6ecea70f user: dkf
06:32 • Closed ticket [745851ffff]. artifact: 414f7e5910 user: dkf
2004-03-02
04:24 • Ticket [745851ffff]: 4 changes artifact: 5b6b10f504 user: dkf
2004-02-29
03:54 • Add attachment tcl-dict-merge.patch to ticket [745851ffffffffff|745851f... artifact: c0b51b4714 user: jenglish
03:54 • Ticket [745851ffff] dict merge status still Open with 4 other changes artifact: 01b21a8000 user: jenglish
03:54 • Ticket [745851ffff]: 4 changes artifact: d6ef336997 user: jenglish
2004-02-21
19:39 • Ticket [745851ffff]: 4 changes artifact: e56d46db0c user: dkf
08:34 • Ticket [745851ffff]: 5 changes artifact: af33d73bf0 user: jenglish
2003-09-07
23:45 • Ticket [745851ffff]: 4 changes artifact: a96fe60e80 user: jenglish
03:16 • Ticket [745851ffff]: 4 changes artifact: 85f2341501 user: dkf
2003-09-06
08:48 • Ticket [745851ffff]: 4 changes artifact: 312534f929 user: jenglish
08:48 • Add attachment tcl-dict-merge.patch to ticket [745851ffffffffff|745851f... artifact: 897f94f4a3 user: jenglish
2003-05-31
00:42 • Ticket [745851ffff] dict merge status still Open with 4 other changes artifact: f86c34bed6 user: jenglish
2003-05-30
15:48 • Ticket [745851ffff]: 4 changes artifact: 378ff5298c user: dkf
08:17 • Ticket [745851ffff]: 4 changes artifact: 53a1dd506b user: jenglish
01:15 • New ticket [745851ffff]. artifact: 5b92aa5e88 user: jenglish

[dict merge]

Closed745851ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 17 years ago
Descriptionplain2003-05-30 01:15:36
A useful addition to [dict] would be a command that
sets multiple dictionary entries in a single step:

dict merge $dictval1 $dictval2 ? ... $dictvalN ?

which would return a copy of $dictval1, extended or
modified by the values in $dictval2.  If multiple
arguments are supplied, values in later arguments
override those in earlier ones.

That is:

proc dict_merge {dict1 args} {
     foreach dict $args {
        foreach {key value} $dict {
            dict set $dict1 $key $value
        }
     }
    return $dict1
}

(This was discussed on tcl-core, but the vote on TIP
111 was already in progress so it didn't make it into
the initial implementation.)
jenglishplain2003-05-30 08:17:05
Logged In: YES 
user_id=68433


[dict merge] can also be expressed as:

    [eval dict replace [list $dict1] $dict2 ... $dictN]

but I'd prefer a solution that doesn't involve [eval].

A variant form that operates on dictionary variables would
also be useful (don't know what to call it though.)
dkfplain2003-05-30 15:48:44
Logged In: YES 
user_id=79902

A better implementation might go like this:

proc dict_merge {dict1 args} {
   foreach dict $args {
      dict for {key val} $dict {
         dict set dict1 $key $val
      }
   }
   return $dict1
}

However, would using C for this purpose be a significant
advantage?  It increases the maintenance load, after all...
jenglishplain2003-05-31 00:42:02
Logged In: YES 
user_id=68433

> would using C for this purpose be a significant
advantage?  
> It increases the maintenance load, after all...

So that it could be spelled [dict merge] instead of
[dict_merge] :-).  There are minor efficiency issues as
well.

But mostly, this is a useful primitive operation on
dictionaries that (I feel) should be part of the core
interface.
jenglishplain2003-09-06 08:48:49
File Added - 60765: tcl-dict-merge.patch

Logged In: YES 
user_id=68433

(Let's see if SourceForge is working today...)

Attached patch implements [dict merge].  Documentation and
test cases still missing.

This might also be a useful thing to have in the C
interface, should refactor.

PS: for a real-live, actual use case, see  'proc myReturn'
in the EXAMPLES section of return.n (r1.7), which currently
uses:

    set options [eval [list dict create -level 1] $args]

This could be replaced with

   set options [dict merge {-level 1} $args]
dkfplain2003-09-07 03:16:36
Logged In: YES 
user_id=79902

Good use case.  Just what I wanted. I'd be happy to support
a TIP.

(I'm less certain about the C interface.  Merging is a
pretty trivial thing to do with the current C API after all.)
jenglishplain2003-09-07 23:45:21
Logged In: YES 
user_id=68433

Argh, does this really require a TIP?  I was under the
impression that since [dict] has not yet appeared in an
official release that the details were still fluid.
jenglishplain2004-02-21 08:34:16
Logged In: YES 
user_id=68433

Just found myself wanting [dict merge] again; raising the
priority.
dkfplain2004-02-21 19:39:00
Logged In: YES 
user_id=79902

Patch review: Shouldn't duplicate the first dictionary
piecemeal.  Better to insert the values into that (if
shared, use the standard Tcl_DuplicateObj idiom to unshare it).
jenglishplain2004-02-29 03:54:29
Logged In: YES 
user_id=68433

Updated patch including suggestion below (though I'm not
sure if this isn't a premature optimization; it won't affect
the asymptotic runtime in normal circumstances).  Also
includes documentation and a rudimentary update to the test
suite.
jenglishplain2004-02-29 03:54:30
File Added - 78372: tcl-dict-merge.patch
dkfplain2004-03-02 04:24:40
Logged In: YES 
user_id=79902

It won't make *any* performance difference except in the
case where someone hands in an unshared object, and then
(especially if the object was large) the difference is going
to be much more significant.  Many list commands (and other
dict subcommands) behave this same way; it is a common core
idiom and there are coding patterns that take advantage of
it to good effect.

Forcing a full copy is just unfriendly.
dkf Closed ticket [745851ffff]. [History diff]2004-03-13 06:32:59
dkfplain2004-03-13 06:33:00
Logged In: YES 
user_id=79902

TIP was accepted and implemented.

Although it was an inspiration, I didn't use the submitted
patch because:
 * The implementation had problems triggerable by attempting
to merge non-dictionary values.
 * The documentation wording wasn't quite clear.
 * The test suite was too incomplete for my taste (I prefer
to test as many failure modes as possible.)
dkfplain2008-12-07 18:30:38
data_type - 210894
dkfplain2009-07-29 19:41:30
IP - Comment Removed: 130.88.1.31
Log in (anonymous is fine) to add a comment.

Attachments: