0

As I mentioned on the title how can i write a generic class and limit the type is concrete(bounded)? I know there is a reified keyword in Kotlin but it's only available with inline function..Is there any way to write like C# in Kotlin with instead of class of Any?

C# Code Example

public class Something<TType> where TType : class
{
    TType Add(TType ttype){
        return ttype;
    }
}

Kotlin Code Example (trying to write equivalent code of C#)

open class Something<TType: class> 
{
   fun Add(ttype: TType): TType {
     return ttype
   }
}
bkulaksiz
  • 77
  • 1
  • 9
  • 1
    I've never written a line of Kotlin, but a quick peruse of their documentation shows they support [generic constraints](https://kotlinlang.org/docs/reference/generics.html#generic-constraints), and they aren't too far off from how to do it in C# (most likely one of the many shared language features borrowed Java :)) – James Apr 26 '20 at 19:41
  • Your C# code is bounding `TType` to things of non-nullable reference type, which is equivalent to `TType: Any` in Kotlin. You ask "Is there any way to write like C# in Kotlin with instead of class of Any?"...but that's a bit confusing to me, as `Any` is the equivalent in Kotlin. – Travis Gockel Apr 26 '20 at 22:22
  • `: class` does _not_ ensure "the type is concrete(bounded)"; it allows both interfaces and abstract types. – Alexey Romanov Apr 27 '20 at 06:42
  • @James There are some specific constraints C# added which aren't in Java, and Kotlin doesn't support those either. Strictly speaking, `: class` would be one of them, but it does happen to have an equivalent. – Alexey Romanov Apr 27 '20 at 06:44
  • Hello dear friends! Actually I was trying to implement C#'s default keyword in Kotlin with Kotlin's reflection. I already have created another post for 2 weeks ago. [Link is here.] We are in the same boat @AlexeyRomanov. I have found some articles and reflection utils which is written in kotlin. I'm so close to do what I want but I have problem with reified function calling as property backfield. If somebody interests, i can share my codes. I really need help about this issue. Thanks. (https://stackoverflow.com/questions/61159046/is-there-any-equivalent-of-cs-default-keyword-for-kotlin) – bkulaksiz Apr 27 '20 at 07:36

1 Answers1

0

The relevant documentation has already been mentioned in the comments. Buy to add a relevant code snippet:

class Something<T: SomeClass> {
    fun add(type: T): T { // type boundary on T doesn't have to be repeated after class declaration
        return type
    }
}
ryfterek
  • 669
  • 6
  • 17
  • that's not his question. In C#, class in this case means that it is limited to reference types. Your example restricts it to a specific class. The answers are already provided in the two comments to his question, T is Any, and Any is already equivalent of class in C#. In other words, in Kotlin, by default, you get the same behavior as if you added class restriction in C#. – Dmitri Apr 26 '20 at 23:02