开发者

Haskell typeclass hierarchy issue

I have a scenario that involves type classes and I'开发者_如何学JAVAm 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜