stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

File objects all hash to the same value

Open wezm opened this issue 4 months ago • 1 comments

I tried using some Files (obtained from the files attribute 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) as the key instead.

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

wezm avatar Feb 25 '24 01:02 wezm

Gleam uses structural equality and hashing except for a few known mutable JS data structures, which get special cased.

We should document this behaviour for JS objects which are the same structure but may otherwise be considered different.

lpil avatar Feb 26 '24 20:02 lpil