generics not able to use functions of superclass in dart/flutter - flutter

I have a problem with the syntax in Dart. I want to be able to use a constructor on a generic class. So I let the generic class extend an abstract class which has the specified constructor. But the Code still shows me that it's not working. Does anyone have an idea?
T fetchItem<T extends JsonModel>(){
return T.fromJson();
// This line shows the error
// The method 'fromJson' isn't defined for the type 'Type'.
}
abstract class JsonModel {
JsonModel.fromJson();
}
The following solution works, but I think it's extremly ugly:
T fetchItem<T extends JsonModel<T>>(T t){
return t.fromJson();
}
abstract class JsonModel<T> {
T fromJson();
}

Constructors are not inherited. Just because a base class has a certain constructor, does not mean the derived class has that constructor.
It doesn't have anything to do with generics. If you extended a class X from your JsonModel, it simply would not have a constructor of that name.

Related

Generic Programming: How can I access a class "fromMap" factory constructor from a Generic class

How to access a class "fromMap" factory constructor from a Generic class.
For example, Here is a very simplified class and method:
abstract class DBbase<T> {
dynamic convert(dynamic input) {
T u = T.fromMap(input.toMap());
return u;
}
}
class DB<UserModel> extends DBbase {}
The **"T.fromMap" ** doesn't work a although the "UserModel" does have the method within.
If I change that line to:
UserModel u2 = UserModel.fromMap(input.toMap());
then it works, but this defeats the purpose of defining the type as a generic.

Swift (4) cannot cast to generic base class?

In Swift, if I declare a class based on generic type:
class BasicRequest<T>{
}
class StartRequest: BasicRequest<BasicPayload> {
}
And then try to use generic method:
class SocketClient {
public func send(request: BasicRequest<Any>){
}
}
It does not compile:
SocketClient.shared.send(request: StartRequest())
with message
Cannot convert value of type 'StartRequest' to expected argument
type 'BasicRequest'
Why actually? The type info should be available across the inheritance chain.
Generics are invariant in Swift, which in your case means that BasicRequest<Any> is not compatible with BasicRequest<BasicPayload>, even if BasicPayload is compatible with Any. This is how invariants work, there's little you can do in this direction.
What you can do, is to make send generic, thus allowing any kind of BasicRequest instances to be passed:
public func send<T>(request: BasicRequest<T>){
}

Using final for subclass in swift

I have two class A , B. B is the subclass of A. Can I use final for class B. I want to reduce the dynamic dispatch. What if there is method in class b that is overriding the class A method . How method dispatching will work?
class A {
///
}
final class B : Class A {
}
Yes, You can
Swift gives us a final keyword just for this purpose: when you declare a class as being final, no other class can inherit from it. This means they can’t override your methods in order to change your behavior – they need to use your class the way it was written.
The final keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection.
So you can use it.

Swift Generics - Allow subclasses passed as parameters

I'm wondering if it's possible to allow a method to take in any subclass of a generic superclass. I've posted example code of what I'm trying to achieve below. Hopefully that describes my situation a little better.
import UIKit
class SuperDataClass {
}
class SubDataClass: SuperDataClass {
}
class SuperClass<T: SuperDataClass>: UITableViewCell {
}
class SubClass: SuperClass<SubDataClass> {
}
class Example {
func test(object: SuperClass<SuperDataClass>) {
print(object)
}
}
let object = SubClass()
Example().test(object: object)
When running this in a playground you'll get the following error:
Playground execution failed: error: Generics.playground:20:24: error: cannot convert value of type 'SubClass' to expected argument type 'SuperClass<SuperDataClass>'
The reason, as far as I can tell, is that the data type is a subclass of the data type expected by the method.
Changing line 15 to
class SubClass: SuperClass<SuperDataClass> {
Will fix the issue but isn't applicable to the scenario I'm trying to achieve. I'd like the method to treat any object that is subclassing SuperClass to be handled by this one method. Is this possible? Is there a better setup that I should be using in order to achieve this?
Thank you.
Make test generic:
class Example {
func test<T>(object: SuperClass<T>) {
print(object)
}
}
SuperClass < SuperDataClass > is not a super class of SuperClass < SubDataClass >, it is a different class using the same generic but not the same associated type.
The fact that the generic's associated type is a subclass of the other does not implicitly make the generic class its ancestor.
For example, if you were to override a method in SuperClass < SubDataClass > (or SubClass), you would be overriding SuperClass < T >, not SuperClass < SuperDataClass >.
In the generic's declaration < T: SuperDataClass > is a constraint for the use of SuperClass, not a hierarchic relationship.

Swift Syntax Meaning "public class Signal<T> { }"

I am seeing codes like below:
public class Signal<T> {
...
}
what does <T> mean?
That's what's known as a Generic. It means that the Signal class can operate on multiple types. For instance, you could instantiate an object of type Signal<String>, or perhaps of Signal<Foo>, or any other type.
Generics allow the same code to work with lots of different types of data, and still retain type safety.