gleam-lang / gleam-lang/stdlib

File objects all hash to the same value

Open
#527 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Gleam
Stars
710
Forks
225
Avg merge
5h 52m
Merged PRs (30d)
3

Description

I tried using some [Files](https://developer.mozilla.org/en-US/docs/Web/API/File) (obtained from the [files attribute](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/files) of `input type="file"`) as keys in a `Dict` but despite inserting multiple different files there was only one entry at the end. Turns out they're all considered the same key. `typeof file` returns `"object"` which means we go to `hashObject`. I think it ends up in the `else` case:

https://github.com/gleam-lang/stdlib/blob/6a701d1b1943891ee866e22c5d84800ade8c09f1/src/dict.mjs#L106-L113

The issue is that `Object.keys` returns `[]` for all files.

Screenshot from 2024-02-25 11-39-26

It seems that if it went through `hashByReference` it might work but not sure how to resolve this more generally. I also note that `hashObject` does a check for a `hashCode` function. I suppose another option, at least for my project would be to add that to the `File` `prototype` or wrap them in a class that does. 🤔

**Edit:** As a workaround I might be about to use the value returned from [URL.createObjectURL(object)](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static) as the key instead.

Gleam version: gleam 1.0.0-rc2
Target: javascript
Env: browser

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.