1

I'm creating a hash map and I'd like to use markers as keys. I set the hash map equality test to "equal", but the last line from the below code is returning ("bye" "fly") instead of ("bazooka" "joe"). Any idea what I'm doing wrong?

(let* ((tmp (make-hash-table :test 'equal))
       (marker-1 (make-marker))
       (marker-2 (make-marker)))
  (set-marker marker-1 1)
  (set-marker marker-2 1)
  (message "markers equal 1?: %s" (equal marker-1 marker-2))
  (message "markerp 1: %s, 2: %s" (markerp marker-1) (markerp marker-2))
  (puthash marker-1 (list "hi") tmp)
  (puthash marker-2 (list "bye" "fly") tmp)
  (message "markers equal 2?: %s" (equal marker-1 marker-2))
  (puthash marker-1 (list "bazooka" "joe") tmp)
  (gethash marker-2 tmp))
6
  • 2
    Looks like a bug, to me. The doc says that if the objects are equal then their sxhash-equal values are the same. But in this case the markers are equal and they have different sxhash-equal values. If someone doesn't provide a good answer here, consider filing a bug report: M-x report-emacs-bug.
    – Drew
    May 2 at 15:20
  • 1
    From (elisp) Defining Hash: " If two objects OBJ1 and OBJ2 are ‘equal’, then ‘(sxhash-equal OBJ1)’ and ‘(sxhash-equal OBJ2)’ are the same integer." But that doesn't seem to be true here.
    – Drew
    May 2 at 15:30
  • 1
    @Drew Indeed, looking briefly at the source code of 27.2, sxhash has a comment about Lisp_Vectorlike objects “Others are equal' if they are eq'”, but that doesn't match with internal_equal which several extra cases with comparisons by value: markers, overlays and window configurations. May 2 at 16:27
  • 2
    Note that using markers as keys in a hash table with equality by value is fragile: it can only work if the markers' positions don't change throughout the lifetime of the hash table. So you need to make sure the hash table isn't kept around past a call to set-marker or a modification of the buffers or other things that can change the markers' positions. May 2 at 16:29
  • @Gilles'SO-stopbeingevil': Yes, your last comment is a good one. (But I assume the OP was giving a test case, just to ask about the equal test when they do have the same position.)
    – Drew
    May 2 at 16:36

1 Answer 1

1

I reported this problem to emacs-devel@gnu.org.

@Stefan Monnier replied that this seems to be fixed in the development version (master) for Emacs 29.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.