Allow bounds on typealias type parameters

Would it be possible to allow bounds on typealias type parameters?

typealias UseCase<T : Result> = (String) -> T

I need this for a generic functional type with bounded type parameter, which should be passed to non-generic classes (with concrete type argument):

data class SystemData : Result

class Presenter(val useCase: UseCase<SystemData>)

But sometimes I don’t know the exact result type so I will use a star projection:

class Presenter(val useCase: UseCase<*>)

And I still want to be sure that UseCase will return a Result.

13 Likes

I am also interested in this - I am trying to build a generic functional API without requiring a huge investment in boilerplate interfaces/classes. Is there a fundamental reason this is not possible?

An abstract example:

interface Foo

typealias Clause<T> = (T) -> Boolean
typealias FooClause<F : Foo> = Clause<F>

Instead, I am forced to create each of these types as their own interface which is quite tedious and difficult to scale. Scaling in my case is having several different frameworks using this base “clause” concept with their own specific type bounds. If typealiases supported type parameter bounds, this becomes trivial. As it is, I have to explicitly create subinterfaces (and potentially flatten the hierarchy, if I add specializations of the base Clause type, into each consumer implementation)

I wish I had noticed this request earlier this year - I’ve been looking for a solution for a while (since May) but hadn’t noticed any other similar requests – now I’ve seen this and several other posts asking for the same thing.

3 Likes

This seems like a blind-spot in the language. Would love to hear how this could be added.

1 Like

I’m still interested in a response, and it’s been a year. I guess I’ll have to follow some means other than the official forums.

3 Likes

I’m also very interested in this feature

Note that with fun interfaces your use case does become simpler now.