Charles Durham
Charles Durham

Reputation: 1707

Haskell typeclass hierarchy issue

I have a scenario that involves type classes and I'm not quite sure how to go about solving it.

I have

class Event a where
     timestamp :: a -> UTCTime
     rawData :: a -> ByteString

class Something a where
    something :: a -> SomethingElse

In my code, I want to create an object that implements both Event and Something. However, in certain cases, the function something is going to need the return from a call to rawData to construct the SomethingElse object. I was wondering if there was to structure these type classes to be able to build a function like

convert :: (Event a, Event b, Something b) => a -> b

being able to call convert x :: (Instance of something) in order to convert, a bit like how binary get is used.

I realize that this is a rather vague description, but please let me know if I can add anything else.

Thanks

Upvotes: 1

Views: 286

Answers (1)

Ankur
Ankur

Reputation: 33637

In type class Something you need to make sure that the type a has implemented type class Event, hence the definition of Something becomes:

class Event a => Something a where
     something :: a -> SomethingElse

Upvotes: 2

Related Questions