Objectype as function parameter

hello,

i’m just starting to work with kotlin. my first impressions are really good.

I’m trying to build some kind of store. Part of the store is an objecttype, which is part of the key. Technically is the store hold by an mutableMap.

The objecttype should by provided by an function parameter. But this is my problem. I’m looking for a possibility to only accept objects which are derived from an specific abstract class. Additionally this abstract class needs an generic parameter.

I try something like

fun add(aggrgegateType: Any<Aggregate<Any>>.....)

or with coherence

fun add(aggregateType: Aggregate<Any>......)

Do you have some input for my problems?

Thank you!

You could write something like

fun <T: Base> add(aggregateType: T)

That way you can only pass in objects that inherit from Base.
You say that the abstract class has a generic parameter so this would look more like

fun <T: Base<Any>> add(aggregateType: T)

or if you need the generic parameter type for something else in the function

fun <P, T: Base<P>> add(aggregateType: T)
2 Likes

Ah, thanks for your kind answer. It helps me a lot.
In the end i struggled most with the an Any-Paramter.

In my opinen it is confusing that Any isn`t covariant automatically