Open chshersh opened 5 years ago
Some updates on this issue:
I have taken a stab at implementing this and my wip can be found in the branch arbus/26-Add-Dict-and-Set-primitive
This is straightforward for types of (Elm a) => Map Text a
but a serious pain for anything else. Even for straightforward enum types that would normally serialize to just plain text, it can't be decoded on the elm side because elm requires its Dict
types' keys to be of type comparable
which is a special Ord,Eq
constraint which only built-in types adhere to and we cannot extend it to support our custom types as well.
I'm not sure how useful the idea of implementing this only in the scenerio where the map key is a Text
is useful.
@arbus Am I understanding correctly that the main problem here is that you can't have data types like Map MyEnum a
or Set MyEnum
in Elm because enums in Elm doesn't have required built-in interfaces implemented? So even if you can write decoder and encoder for such Map
, you can't use it in Elm code, it will be a compile-time error, right?
Yes that is correct, only things that are comparable
can be the key of a dict or be put in a set.
From the documentation:
A dictionary mapping unique keys to values. The keys can be any comparable type. This includes Int, Float, Time, Char, String, and tuples or lists of comparable types.
--
A set of unique values. The values can be any comparable type. This includes Int, Float, Time, Char, String, and tuples or lists of comparable types.
@arbus Okay, in that case I propose to put this issue on hold and keep your branch. It contains useful code for this feature. I also think that using Map Text a
on frontend is awkward and will complicate our code. Maybe somebody will implement enum-map
package with JSON encoders and decoders. Or maybe elm-0.20
will provide these builtin instances for enums. Who know in what ways our lives will be improved...
For the record these 2 packages (of mine, sorry for shameless promotion) are solving problem with comparable
Anyway, generating code for this would be still challenging become it would require generating key -> comparable
functions.
My suggestion would be to:
IntSet
IntMap
List
for Set
List (key, value)
for Map, Hashmap and AssocList.
Elm has standard data types called
Dict
andSet
for container packages:It's probably a good idea to support them too for common Haskell containers.