haskell-works / hw-kafka-client

Kafka client for Haskell, including auto-rebalancing consumers
MIT License
139 stars 50 forks source link

Wrap callbacks in newtype to make wrong usage harder #161

Closed phile314 closed 3 years ago

phile314 commented 3 years ago

I was at first struggling to use the callbacks correctly. Granted, this was mostly my fault as I looked at the wrong hackage version of hw-client which did not yet have the examples in the doc on how to use the callbacks. But anyway, I find having the ... -> k -> IO () in the type signature a bit confusing. I was basically doing:

producer <- newProducer ...
statsCallback cb producer

instead of using setCallback and the producer properties.

I think just wrapping the callbacks in a newtype makes it hard (impossible?) to make this mistake and improves readability. What do you think?

PS: Backward-compatiblity is only affected if people gave explicity type signatures for any variable containing the callback. If one just directly passes the callback to setCallback no change is required. E.g.:

  -- Global producer properties
  producerProps :: ProducerProperties
  producerProps = brokersList [BrokerAddress "localhost:9092"]
               <> sendTimeout (Timeout 10000)
               <> setCallback (deliveryCallback print)
               <> logLevel KafkaLogDebug

stil compiles.

AlexeyRaga commented 3 years ago

Thanks guys, I will review it soon!

AlexeyRaga commented 3 years ago

Looks good to me! Thanks @phile314, I like it! 👍

If you could rebase (I merged a couple of your other PRs) and resolve conflicts then we can hit the green button and make a release!

phile314 commented 3 years ago

Rebased on current master.

And thanks already for making a release with these changes, it is greatly appreciated :+1: