I want to be able to store several different types of lists in a single
data structure, and and each type of list should be treated differently by
the view function.
What I've tried:
type ThingList
= FooList (List Foo)
| BarList (List Bar)
| BazList (List Baz)
This works okay, but if I want to do something like get the length of a
ThingList without caring about whether it is a FooList, BarList or BazList
it seems I have to have to write something like this:
length : ThingList
length thingList =
case thingList of
FooList list ->
List.length list
BarList list ->
List.length list
BazList list ->
List.length list
Since in my actual program I plan to have several more sub-types in the
actual counterpart to ThingList this becomes somewhat cumbersome and it
definitely isn't DRY. I guess this is a form of the expression problem?
<https://en.wikipedia.org/wiki/Expression_problem> I tried to generalize it
a bit so I wouldn't have to update as many of these functions when I add a
new sub-type like this:
reduce : (List a -> b) -> EntityList -> b
reduce f entityList =
case entityList of
FooList list ->
f list
BarList list ->
f list
BazList list ->
f list
but that gave me an error like this:
Function `f` is expecting the argument to be: List Foo But it is: List Bar
In my particular case, at least for now, I only wanted the length of a
ThingList literal so I can work around it, but I'm curious if there is way
to structure the types that avoids this problem.
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.