When using -XOverloadedStrings
you can implement IsString
which only requires a function fromString
.
Now if you want to use string literals for pattern matching, you also have to implement Eq
, which makes sense:
f :: MyString -> Bool
f "foo" = True
f _ = False
-- equivalent to
f x
| x == fromString "foo" = True
| otherwise = False
But then why does the IsList
type class which is used with -XOverloadedLists
require that you implement toList
?
In the wiki the only mentioned use-case of toList
is pattern matching.
And I get that Eq
is not sufficient for list pattern matching.
But then toList
should be in a different type class that's only required if you'd like to use list pattern matching with your type, just like IsString
does not require Eq
.
The annoying thing about this to me is that the condition fromList . toList = id
has to be met but this simply can't be guaranteed for some types like e.g. an unordered collection which make no guarantees that the order of elements is kept.
This just seems very inconsistent.
[1, 2, 3, 4]
in a context where the result of that expression was an unordered collection, I'd have some unfortunate words for the developer of that code – Aloise