Some help with generics


#1

I have the following code:

public class AnotherClass<L> {   public fun doStuffTo(obj: L) {   ...   } } public class MyClass<T:MyClass<T>> : Cloneable {   val anotherClass:AnotherClass<T>;

  public fun doStuff() {
  anotherClass.doStuffTo(this as T);
  }

  public fun clone(): T {
  val result = super.clone() as T
  …
  return result;
  }
}


I’d like to remove the requirement of having a recursive generic and casting this to T, if possible. I’m adding it just to reference the current instance class, like in Java. But maybe there is a better way of doing this in Kotlin?


#2

It could be that I don't understand you correctly, but is this what you are looking for?

fun main (args:Array<String>) {   MyClass(AnotherClass()).doStuff() }

public class AnotherClass<V> {
  public fun doStuffTo(obj: V) {
  println(obj)
  }
}
public class MyClass(private val anotherClass:AnotherClass<MyClass>)  {
  public fun doStuff() {
  anotherClass.doStuffTo(this)
  }
}