Git Product home page Git Product logo

Comments (7)

jkpl avatar jkpl commented on August 24, 2024 1

The TypeTaggedTrait is there to add the type tag derived from the extending class as a value. This allows preserving the type information in the runtime that would be otherwise lost due to type erasure. This is especially nice when combined with Akka actors where each message loses its type information. The problem that TypeTaggedTrait has is that it doesn't deal very well with inheritance: the cast operation matches on exact type rather than being aware of the type hierarchy.

Here's some additional reading on the subject, if you're interested: https://www.cakesolutions.net/teamblogs/ways-to-pattern-match-generic-types-in-scala

from scala-kafka-client.

simonsouter avatar simonsouter commented on August 24, 2024

Try moving the definition of the MyDummy case class above the test case definition, to avoid this problem.

from scala-kafka-client.

joesan avatar joesan commented on August 24, 2024

Cool! That got rid of the problem. But is there an explanation as to how this solves the problem? Curious to know?

from scala-kafka-client.

simonsouter avatar simonsouter commented on August 24, 2024

Not exactly sure. The TypeTag feature relies on reflection and implicits, which are prone to this kind of weirdness...

from scala-kafka-client.

joesan avatar joesan commented on August 24, 2024

How could I understand the TypeTaggedTrait? The code looks super simple and awesome but I can't reason out what this trait is actually doing! Could you please give me a short explanation?

from scala-kafka-client.

joesan avatar joesan commented on August 24, 2024

I do understand the TypeTag in Scala and why it is needed. I wanted to understand what roles does the TypeTaggedTrait play in the application. For example., the ProducerRecord usage is like this:

final case class ProducerRecords[Key: TypeTag, Value: TypeTag](
  records: Iterable[ProducerRecord[Key, Value]],
  successResponse: Option[Any] = None,
  failureResponse: Option[Any] = None
) extends TypeTagged[ProducerRecords[Key, Value]]

How does each of the methods implemented in the TypeTaggedTrait relate to the ProducerRecords?

from scala-kafka-client.

jkpl avatar jkpl commented on August 24, 2024

It's there so that the actors that receive those records (in this case the producer actor), can pattern match on only ProducerRecords[MyKey, MyString] and not on any other ProducerRecords with different type parameters.

The pattern match for the producer actor is effectively a function from Any => Option[ProducerRecords[MyKey, MyString] where MyKey and MyString are any custom type parameters. If the input is of type TypeTaggedTrait, then we know that it has a cast method. We can use that cast method to attempt to cast the value to ProducerRecords[MyKey, MyString], which will work if only if the value is of type ProducerRecords and it has the MyKey and MyString type parameters. Otherwise it will return None, i.e. it's not a match. See TypeTaggedExtractor for the implementation details.

from scala-kafka-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.