is there a way in typescript to extend a class in this way:
class ChildClass<Wrapper<A>> extends SuperClass<A>
This doesn't work but the idea would be to wrap the generics type into a known construct. Here are the docs:
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md
This sounds a bit similar to this issue:
Can you subclass a generics class with a specific typed class?
I don't do much oop so I'm not very familiar with stuff like covariance and contravariance, any help would be appreciated.
It maeks no sense to write ChildClass<Wrapper<A>> before the extends, because there you declare the generic type parameters. That means you can give them a name and, if you need to, a constraint (for example ChildClass<A extends Wrapper>). What does Wrapper<A> mean in this context? The compiler can make no sense of it.
What is absolutely possible is to use Wrapper<A> on the other side of the extends, because there A is not a (formal) type parameter, but a type argument. That means you are using the type parameter previously defined and there you can generate new types with it.
So depending on what you actually want to do, there are two options for you:
A is assignable to Wrapper
When you want to make sure the A is a Wrapper or a derived class, use a generic constraint:
class ChildClass<A extends Wrapper> extends SuperClass<A>
A is a type argument of Wrapper<>
If Wrapper<> is itself a generic class or interface and you want to use A as its type argument, do this:
class ChildClass<A> extends SuperClass<Wrapper<A>>
Related
I have a generic parent class:
open class Parent<T>{/*...*/}
and I have some derived classes that implement a specific instance of the generic parent:
class Derived1 : Parent<Foo1> {/*...*/}
class Derived2 : Parent<Foo2> {/*...*/}
where Foo1 and Foo2 are some classes defined elsewhere
I now need to create a function that returns a different derived class based on some input parameter:
fun getDerived(derived: SomeEnumType): Parent{
//return the correct derived class
}
Of course the line above won't compile because Parent requires a generic parameter. The Derived classes are not of the same type, so I wouldn't expect to be able to handle this polymorphically. How can I achieve this? I am familiar with kotlin.Any but this seems like cheating.
If it helps, I am using this pattern to parse json in the correct child class with the gson library (by overriding the deserializer)
You could get away with Parent<*> but if there is a relationship between Foo1 and Foo2 (e.g extending a common interface, Buzz) then you could use something like Parent<out Buzz>.
IIRC, <*> is like Java's wildcard <?>. Not bounding the response type will mean you'll need some type guards at the call site of your function getDerived to make the response inspectable.
In the Kotlin docs, they show how to include type parameters:
class Box<T>(t: T) {
var value = t
}
This is a simple example. But I've come across one that looks like this:
abstract class SomeAdapter<T, WH: SomeViewHolder>(private val viewModel: SomeModel<T>?) {
}
How do I interpret this? Do I interpret this as:
SomeAdapter takes two parameters when it's instantiated - a T and a WH. And the constructor takes a viewModel.
As you already referenced, this class has two generic types: T and WH. The latter does specify an upper bound SomeViewHolder which will only allow sub types of that upper bound to be used as the generic type WH.
Since your title goes:
Understanding generic parameters in an abstract class
the question at hand is: Would it be different (regarding the generic types) if SomeAdapter would not be abstract. The answer is: No.
In this particular example T can be Any? and WH can be any subclass of SomeAdapter or SomeAdapter itself (if SomeAdapter is not abstract).
The types of T and WH are fixed at compile time (see Type erasure).
So, you have to see generics like a variable for a type.
I know there are various capabilities in Java with reflection.
For example:
Class<?> clazz = Class.forName("java.util.Date");
Object ins = clazz.newInstance();
I wonder if I could pass class dynamicaly in some method declaration in <> tags (or there is other way to do it if it must be fixed). I would like to change that class declaration dynamicaly; because I would like to write generic method for all types of classes.
In there, I have this:
List<Country>
Can I write it something diffrent with reflection? For example can it be somehow be achieved to pass class as parameter (or how else should be this done):
List<ins>
? I would appreciate examples.
This cannot be done because generics are a compile time feature. Once code is compiled, the only place where generics are exists are at method signatures, and they are only used for compiling new code.
When working with reflection, you are basicly working with raw types, and need to code according to that, that means, you can cast the returned result of newInstance() to the list type your need, for example:
List<Country> ins = (List<Country>)clazz.newInstance();
This is a safe operation to do, because you know at that point its empty, and isn't passed to any outside code.
I don't think this is possible. Generics in Java are implemented in a way that prohibits runtime access.
Generics are there so that the compiler can verify correct typing, but are no longer present at runtime (this is called "type erasure"). Reflection deals with the runtime representation of types only. As far as I know the only case where reflection has to deal with generics is to find out "fixed" type parameters of sub-classes, e.g. when you have class Bar<T> and class Foo extends Bar<String>, you can find out that the T of Bar is fixed to String in Foo using reflection. However, this is information found in the class file, too. Except that, reflection can only see or create raw-types.
I have a program ZPROG1_TEST where I define a local class LCL_PROG1_HELPER.
I have a second program ZPROG2_TEST where I'd like to define a variable reference to this class.
Isn't there a syntactic possibility for me to do this?
Or could this be in theory doable with the RTTI classes like CL_ABAP_CLASSDESCR ?
EXTRA
Why I'd like to do this is because I have a custom form ZMM_MEDRUCK that needs to know if the ME32N Document it's printing has been changed but not saved.
I've figures out the exact objects whose properties I need to interogate, but some of them are defined at design time as common interfaces, like IF_SERIALIZABLE_MM, and I need to cast them to the local classes whose instances I know these objects are going to be, like \FUNCTION-POOL=MEGUI\CLASS=LCL_APPLICATION.
I could of course try a dynamic method call and not care about anything, but since i'm here i thought i'd ask this thing first.
You could do it like that.
REPORT ZPROG1_TEST.
INTERFACE lif_prog1_helper.
METHODS:
test.
ENDINTERFACE.
CLASS LCL_PROG1_HELPER DEFINITION.
PUBLIC SECTION.
INTERFACES:
lif_prog1_helper.
ALIASES:
test FOR lif_prog1_helper~test.
ENDCLASS.
CLASS LCL_PROG1_HELPER IMPLEMENTATION.
METHOD test.
WRITE / sy-repid.
ENDMETHOD.
ENDCLASS.
REPORT ZPROG2_TEST.
DATA: g_test TYPE REF TO object.
START-OF-SELECTION.
CREATE OBJECT g_test TYPE ('\PROGRAM=ZPROG1_TEST\CLASS=LCL_PROG1_HELPER').
CALL METHOD g_test->('TEST').
CALL METHOD g_test->('LIF_PROG1_HELPER~TEST').
As far as I know, this is not possible. Accessing the local class dynamically is easy (well, relatively easy), but referring to it statically - not as far as I know. You'll probably have to call the methods dynamically.
How do I get using reflection the most generic type from a shared constructor in the base class :
Public Class Foo()
Shared Sub New()
'Here we have code to get the type!
MethodBase.GetCurrentMethod().DeclaringType
End
End Class
Public Class Bar()
Inherits Foo
End Class
I expect the result to be Bar type and not the Foo. Is it possible?
First, it seems you want to find the most derived type (or the most specific type), not the most generic type -- which would mean rather the opposite (either, that generics are involved, or that the most general type is being sought).
While it may be possible to do this using reflection, your need for it might indicate that you have your class design wrong, or less than optimal.
First, constructors aren't virtual methods, so inside a constructor (IIRC), the Me object reference is of the type that contains this constructor.
What you could do is reflect over all of an assembly's types and find all those that are derived from Foo. You would then have to build a inheritance graph of these types and assign a number to each saying how far it is derived from Foo (number of inheritance levels). You could then check the Me object reference against all of the types you've identified (see if Me can be cast to each of them), and from that subset, choose the one type with the largest number of inheritance levels.
I hope that from this, you'll see that it's probably not worth the effort. It would be more interesting, and probably more helpful, to re-think why you need to do this, and if possible, find a way to avoid it.