Overcoming limitation with polymorphic type in haskell -
i have type so,
data agentspec = agentspec { agent :: , events :: [bytestring] } deriving (show)
and other types so,
data java = java data php = php
i trying create function of type,
allevents :: [agentspec a] -> [bytestring] allevents aspec = concatmap events aspec
but unable create values of type [agentspec a]
if do,
allevents [agentspec java ["event1, "event2"], agentspec php ["event3, "event4"]]
it not typecheck , understandably so. doesn't type check because, agentspec java
not agentspec a
so understand why not work. not know how overcome limitation without writing lot of duplicate code.
one alternatve manually construct list of type,
allevents :: [bytestring] allevents = ["event1", "event2", "event3", "event4"].
but feel i'm rewriting things i've modelled in types. there way make use of existing types want? concatenated list of bytestrings of agentspecs
this code:
allevents [agentspec java ["event1, "event2"], agentspec php ["event3, "event4"]]
doesn't compile because agentspec java ["event1, "event2"]
, agentspec php ["event3", "event4"]
are of different types, , lists in haskell can contain 1 type. agentspec [bytestring]
can created type a
, once created can't mixed values of different type.
i don't know you're modeling, typically i'd recommend this:
data language = java | php data agentspec = agentspec { agent :: language , events :: [bytestring] } deriving (show) allevents = [agentspec java ["event1", "event2"], agentspec php ["event3", "event4"]]
based on comment writing library, though, doesn't sound work. can elaborate on you're trying achieve?
Comments
Post a Comment