Skip to Content
Course content

148: Type Aliases for Complex Generics

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a permission-handling module the other day, and I hit a wall. Not a logic wall—a readability wall. I had this function that returned a map of user roles, where each role mapped to a list of specific permission objects. If those permissions were wrapped in a Result type for error handling, the signature started looking like a soup of angle brackets.

The Wall of Angle Brackets

Let's look at what I had. I tried to write a function to fetch the permission matrix for a specific organization. It looked something like this:

fun getOrgPermissions(orgId: String): Result<Map<String, Map<String, List<Permission>>>>

I stared at that for a second and realized I had to squint just to figure out where the Map ended and the Result began. It's technically correct, and the compiler loves it, but if I'm reviewing this code in six months, I'm going to spend five minutes just parsing the types before I even get to the logic. I tried to keep it as is, but then I had to pass this type into other functions, and suddenly my whole file was just <><><> everywhere.

Fighting the Verbosity

My first instinct was to create a wrapper class. I thought, "Why not just make a PermissionMatrix class?"

class PermissionMatrix(val data: Map<String, Map<String, List<Permission>>>)

But then I realized that was overkill. I didn't need any new behavior, no custom methods, and no state. I just wanted the name. By creating a class, I'm adding a tiny bit of runtime overhead and forcing myself to wrap and unwrap the map every time I want to use standard Kotlin Map functions. It felt like I was building a whole house just because I wanted a fancy name for a room.

The Shortcut: Type Aliases

That's when I remembered typealias. It's essentially a way to tell the compiler, "Whenever I say this word, I actually mean this giant mess of generics."

I tried defining it right above my service class:

typealias PermissionMap = Map<String, Map<String, List<Permission>>>
typealias PermissionResult = Result<PermissionMap>

Now, let's look at that function signature again:

fun getOrgPermissions(orgId: String): PermissionResult

That's infinitely better. I can actually breathe now. The logic hasn't changed, the bytecode is exactly the same, but the intent is clear. I'm not returning a "Map of Maps of Lists"; I'm returning a PermissionResult.

Just a Nickname

Here is the part that tripped me up for a minute: I wondered if I could use this to create a strictly different type. I tried to see if I could prevent a regular Map from being passed into a function expecting a PermissionMap.

typealias PermissionMap = Map<String, Map<String, List<Permission>>>

fun processPermissions(map: PermissionMap) { /* ... */ }

// I tried passing a standard map here...
val rawMap: Map<String, Map<String, List<Permission>>> = mapOf(...)
processPermissions(rawMap) // This works perfectly.

It worked, but not in the way I hoped. I realized that a typealias is not a new type. It's just a nickname. It's like calling "Robert" "Bob"—it's the same person, just a shorter name. If you need actual type safety where the compiler treats two identical structures as different types, you'd need inline value classes, but for cleaning up generic madness, typealias is exactly the tool for the job.




📋 Practical Task

Implementing a UserSessionCache Alias

You are building a caching layer for a session manager. The current implementation uses a deeply nested structure to track sessions: a MutableMap where the key is a UserId (String), and the value is another MutableMap that maps SessionId (String) to a SessionData object.

Currently, the code is cluttered with the following type: MutableMap<String, MutableMap<String, SessionData>>

Your Task:

  1. Create a typealias called UserSessionCache to represent this nested map structure.
  2. Define a class SessionManager that contains a private property of type UserSessionCache.
  3. Implement a function updateSession(userId: String, sessionId: String, data: SessionData) that uses the alias to store the session data in the cache.
data class SessionData(val lastAccess: Long, val ipAddress: String)

// Write your typealias and SessionManager class below
Rating
0 0

There are no comments for now.

to be the first to leave a comment.