Is there a way to define a function that accepts only case classes?

def myFunc[T](a: T): String = ??? 

Is there a way to tell myFunc that T should only be a case class?


I am doing an assignment where I have to accept 2 case classes and if they are not equal print out the possible reason for it. It’s not something that can be fool proof but nevertheless I have been asked to give it a shot. I don’t want the solution to be spoon fed. i want to do it myself. Hence refrained from giving the full context :slight_smile:

No. case class is not a data type. It’s only a way to tell the compiler that you want some special treatment for that class.

But why would you want that? If you say something about the context maybe we can find a better solution?


Not really. All case classes are a subtype of Product though. The library Shapeless leverages this for example to abstract over things with the same shape.

1 Like

Yeah, and moreover are subtypes of Product with Serializable unless something changed in 2.13. I’m not sure what fraction of that pairing are case classes, but it wouldn’t surprise me if it’s very large, so that might be a mostly-working approach for just accepting case classes.

I agree, though, that it’s an unusual thing to want – what’s the use case?


edited the question

edited the question to add more context to it

Given the extra information, the way to go is probably to accept all subtypes of Product. Unless it’s a shapeless exercise.

1 Like