Summary
Unit srfi-69: does not specify action when two merged hash tables contain the same keys
Metadata
- Id: fdbff0bba8087c8b6b1b172d86d77aeed5904a0d
- Trac id: 1290
- Type: defect
- Reporter: nxg
- Owner:
- Cc:
- Status: closed
- Component: extensions
- Estimated difficulty: trivial
- Resolution: fixed
- Priority: minor
- Milestone: 4.13.0
- Version: 4.10.x
- Changetime: 2017-10-12 15:15:56 UTC
- Created: 2016-05-26 18:46:13 UTC
- Keywords: srfi-69
Description
The documentation for `hash-table-merge` says
(hash-table-merge HASH-TABLE-1 HASH-TABLE-2) procedure Returns a new HASH-TABLE with the union of HASH-TABLE-1 and HASH-TABLE-2.
However this does not specify which hash table 'wins' when the two hash tables contain the same key. This procedure doesn't appear in SRFI 69, so that doesn't resolve it.
From experiment, and by inspection of the code, it is clear that hash-table-1 wins:
% csi
CHICKEN
(c) 2008-2015, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.10.0 (rev b259631)
macosx-unix-clang-x86-64 [ 64bit manyargs dload ptables ]
compiled 2015-08-04 on yves.more-magic.net (Linux)
#;1> (use srfi-69)
; loading /Data/tools/chicken-4.10.0/lib/chicken/7/srfi-69.import.so ...
; loading library srfi-69 ...
#;2> (define h1 (alist->hash-table '((a . 1) (x . 2))))
#;3> (define h2 (alist->hash-table '((b . 1) (x . 3))))
#;4> (define h3 (hash-table-merge h1 h2))
#;5> (hash-table->alist h3)
((x . 2) (b . 1) (a . 1))
#;6>
But it would be good to document this, so that the user can know this is not an unspecified behaviour which may change in future. The same goes for
(alist->hash-table '((a . 1) (a . 2)))
Does this produce a hash table with `a` defined as 1 or 2 (it's 1, but the documentation doesn't guarantee this)
Incidentally (and at the risk of putting distinct notes into one ticket), it would be really attractive if this library defined a pure-functional `hash-table-set` which mapped `(hash-table? any any) -> hash-table?`
Changes and comments
[2017-08-25 15:23:27 UTC] sjamaan set difficulty to trivial
[2017-08-25 15:23:27 UTC] sjamaan changed milestone from someday to 4.13.0
[2017-08-25 15:23:27 UTC] sjamaan wrote:
Thanks for reporting this, sorry that it went unnoticed for a while.
I think documenting this would be a good improvement.
Regarding the purely functional API: I don't think that would be a good idea for hash tables, as it implies copying the entire table which is rather expensive (avoiding this would imply a complete redesign). It would only make sense for smallish hash tables, but for those cases an alist will suffice just as well (and may be faster even), and we already have functional APIs for those.
[2017-10-12 15:15:56 UTC] sjamaan changed status from new to closed
[2017-10-12 15:15:56 UTC] sjamaan set resolution to fixed
[2017-10-12 15:15:56 UTC] sjamaan wrote:
Fixed with e311b61770a64fde0ba4503cea5930b4d74679c0 and r34689