Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode.dict & Decode.dict are asymmetrical #17

Open
ghost opened this issue Sep 6, 2019 · 1 comment
Open

Encode.dict & Decode.dict are asymmetrical #17

ghost opened this issue Sep 6, 2019 · 1 comment

Comments

@ghost
Copy link

ghost commented Sep 6, 2019

Hello!

I'm working with dictionaries of character-based keys. Encoding them is straight-forward, as I simply have to convert keys from characters to strings. No problem!

Writing the decoder for this kind of dictionary was more problematic, as I don't have the option convert keys back into characters.

Encode.dict : (k -> String) -> (v -> Encode.Value) -> Dict k v -> Encode.Value

Decode.dict : Decoder a -> Decoder (Dict String a)

I am working around this, but I'd love for the solution to be incorporated into Decode.dict, to avoid the additional passes through the data.

Proposed solution

I'd really like these functions to be symmetrical, in the sense that the dictionary-decoder takes a function to convert the string-key into a comparable value in the same way the encoder requires me to convert the comparable keys into strings.

Perhaps like this:

Decode.dict : (String -> comparable) -> Decoder a -> Decoder (Dict comparable a)

To spare you the time, I could do this implementation for you, but let me know what you think when you have the time ☀️

@davidpomerenke
Copy link

I'm working with a dictionary with Int keys, and thus endorse this feature request.

The solution seems to be already implemented in Decode.Extra.dict2 from the elm-community/json-extra package.

Maybe it would make sense to have the Decode.Extra.dict2 implementation under the name Decode.dict, since it is more symmetrical to Encode.dict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant