Use a Type Alias with a generic type in kotlin?

Viewed 226

Currently I use this type alias

typealias GridMatrix = MutableList<CoordsValue, String?>

Now I want to replace String? with T? because of reason I wanted to do this

typealias GridMatrix = MutableList<CoordsValue, T?>

Unfortunately the compiler does not allow it and therefore I am a bit sad since I liked my typealias here and am using it throughout the codebase.

Is there a way to maybe get this to work somewhat? Workaround or whatever? Also I don't understand why it is not working? I thought of a typealias just as a simple placeholder or replacment like in C++ DEFINE which just replaces the stuff before the real compilation starts.

1 Answers

You can declare a typealias with type arguments.

Try this:

typealias GridMatrix<T> = MutableList<CoordsValue, T?>

Here's the kotlin reference on typealiases.

Related