Nested and inner class extensions

It would be great to be able to create extensions in a form of nested or even inner classes. I was thinking about it recently. I’d like to have an API nicely organized in namespaces but also distributed in many files. So for example, for my business domain which is an User entity I’d like to define its data class as User, and add some components to it as nested classes so I’d end up with: User, User.Service, User.Repository etc. but each of these components would be defined in its own file, so that I can have a domain package myproject.user and 3 files with my component class definitions inside.

My proposal is to make this possible:

myproject/user/User.kt

package myproject.user

data class User(...)

myproject/user/Service.kt

package myproject.user

import myproject.user.User

class User.Service

myproject/user/Repository.kt

package myproject.user
 
import myproject.user.User

class User.Repository

The main purpose I’d use it is to provide more readibility and structure to the code which groups components into domains, so I can associate domain with package/data class, component with class and discover avaliable components by putting a dot after domain type. I can do it now but all of the code for one domain must be contained in a single file. Providing extensions of nested classes would allow me to split one big class into several smaller grouped by packages.

7 Likes