Is there a reason for not allowing inner+data classes?


Can’t see why this is not a “valid” combination.


Inner classes have a hidden reference to an instance of the outer class, from which they were instantiated.
When an inner class is also a data class, this leads to a question, whether that reference is one of the data class component properties?
And whether that reference affects how the functions equals, hashCode, toString, componentN of the data class are generated depends on the answer on this question.


I’m curious: what kind of useful design does this restriction prevent?

My gut feeling is that data classes should stand on their own. If they depend on state of an outer object, they are not really a data classes, right?


Actually this comes from another restriction I’ve asked before in forum: Private classes in “parallel” files in same package

i.e. : I didn’t need the class to be inner : I actually wanted it to be module private, but be named with same name in multiple files, which as mentioned above doesn’t work. So making it inner was just a workaround to avoid the name clash…



You can simply make it nested inside another class. Nested classes can be data classes.


In Java terms these would be static inner classes (have access to private members of the outer class but don’t hold an instance reference)


Thanks. I understand now the difference. Is taking me some time to get familiar with all the new qualifiers… That solves problem.