class A {
void hello() {
print('world');
}
}
class B extends A {
#override
void hello() {
print('StackOverflow');
}
}
(A() as B).hello(); results in type 'A' is not a subtype of type 'B' in type cast.
Full disclosure: I don't know Dart.
You can't do this kind of cast because it might result in function calls or field accesses that aren't well defined.
I'll change your example to demonstrate:
class A {
void hello() {
print('world');
}
}
class B extends A {
#override
void hello() {
print('StackOverflow');
}
void hello2() {
print('Is Great!');
}
}
Now if you do (A() as B).hello2(); what should Dart do? This isn't really obvious, so it doesn't allow you to do that. Going the other way isn't an issue since B inherits all stuff from A.
class A {
void hello() {
print('world');
}
}
class B extends A {
#override
void hello() {
print('StackOverflow');
}
}
class C extends A {
#override
void hello() {
print('Hello');
}
}
Another issue is a value that has type A may be a different subtype of A like C
The way casting works is you can only go from a more specific type, B in this case, to a more general type, A. Your creating an instance of A, but A is not B.
Dart generally allows you to down-cast from a super-type to a sub-type because the value might actually be of the sub-type.
Animal animal = Cat();
if (something) animal = Dog();
...
Cat cat = animal as Cat; // Allowed, the animal may be a cat.
However, Dart 2 disallows down-casts in a few cases where it is obvious (even to the compiler) that the cast will always fail at run-time. That's what you are hitting here: (A() as B). The A() calls a generative constructor, so the compiler knows that the type of that expressions is exactly A (and not any proper sub-type of A). So, it knows that casting that to B will always fail at run-time, and for your own protection, it disallows the program entirely. Hence, a compile-time error.
Related
void foo<T extends num, String> (T t) {
if (t is String) {
String s = t; // Error
}
}
A value of type 'T' can't be assigned to a variable of type 'String'.
You won't be able to do this with base Dart as your generic type T can only extends one class.
The only way I would see such a behavior feasible would be by using a 3rd party packages such as dartz with its Either type.
Example
void foo<T extends num>(Either<T, String> t) {
final String s;
if (t.isRight()) {
s = (t as Right<T, String>).value;
} else {
s = (t as Left<T, String>).value.toStringAsFixed(3);
}
print(s);
}
foo(Left(1.0)); // prints '1.000'
foo<int>(Right('bar')); // prints 'bar'
There is no syntax to specify that a generic type implement multiple interfaces, so there is no way for this to work with compile-time checks.
Furthermore, your particular example can't work because num and String cannot be extended nor implemented, so it's impossible to have a type that implements both.
If we change your example, which relies on a runtime check, to use two custom types, it still won't work:
class C1 {}
class C2 {
void f() => print('C2.f');
}
class C3 implements C1, C2 {
#override
void f() => print('C3.f');
}
void foo<T extends C1>(T t) {
if (t is C2) {
t.f(); // 'f' isn't defined for the type <unknown>
}
}
See https://github.com/dart-lang/language/issues/2047: t isn't related to C2, so the is C2 check unfortunately will not automatically promote it to C2. You instead can use a runtime cast:
void foo<T extends C1>(T t) {
if (t is C2) {
(t as C2).f();
}
}
or upcast to Object/dynamic first:
void foo<T extends C1>(T t) {
Object t0 = t;
if (t0 is C2) {
t0.f();
}
}
But really you should just use T extends C3 if possible.
is it possible to call generic types function.
if not is there a different approach to something like this.
someFunction<T>(){
T.anotherFunction();
}
EDIT
MyModel model = NetworkClient.sendRequest<MyModel>(url);
static Future<T> sendRequest<T>(String URL){
//send request
var res = data.toString();
return T.fromJson(json.decode(res))
}
void main() {
someFunction(Foo());
someFunction(Bar());
}
someFunction<T>(T t) {
if (t is Foo)
t.fooFunc();
else if (t is Bar)
t.barFunc();
else
throw Exception("Unknown type: ${t.runtimeType}");
}
class Foo {
void fooFunc() {
print("foo");
}
}
class Bar {
void barFunc() {
print("bar");
}
}
Put your method (lets say MyMethod) in an interface or base class. Lets say MyInterface.
Then use a constraint on your generic type:
someFunction<T extends MyInterface>(T t){
t.MyMethod();
}
Since you cannot have constructors in an interface, your plan with the fromJson might not work out. I don't know your class structure. But you can write generics with specific constraints in mind.
I have this code which works well on android emulator but gives error on web.
import 'package:parivaar/components/screens/home/Home.dart';
typedef T Constructor<T>();
final Map<String, Constructor<Object>> _constructors =
<String, Constructor<Object>>{};
void register<T>(Constructor<T> constructor) {
_constructors[T.toString()] = constructor;
}
class ClassBuilder {
static void registerClasses() {
register<Home>(() => Home());
}
static dynamic fromString(String type) {
return _constructors[type]();
}
}
And i am calling that function as follows:
class _MyHomePageState extends State {
KFDrawerController _drawerController;
#override
void initState() {
super.initState();
_drawerController = KFDrawerController(
initialPage: ClassBuilder.fromString('Home'),
.
..
...
....
You are probably assuming that T.toString() returns the source name of the type as a string. Nobody ever promised that.
It works on native, but on the web you often optimize for size and "minification" turns all the long names into shorter names. With that, the name of the type Home is no longer the string "Home".
I generally do not recommend depending on the string representation of types (or Type objects for that matter).
Consider changing register and fromString to:
void register<T>(Constructor<T> constructor) {
_constructors[T] = constructor;
}
and
static T fromType<T>() => _constructors[T]();
That relies on Type object equality, which is a well-defined operation.
Not perfect, but still better than going through strings.
If you need to create the objects dynamically from strings, where you don't know the type, then I'd instead require you to provide the key string on registration, changing register to:
void register<T>(String key, Constructor<T> constructor) {
_constructors[key] = constructor;
}
and register types like:
static void registerClasses() {
register<Home>("Home", () => Home());
}
With the below code as an example I can not figure out how to make the generic typed Function work with out casting as shown. Every other way I try I get some variation of
The argument type 'Null Function(Gift)' can't be assigned to the
parameter type 'dynamic Function(T)'
var present = Present<Gift>(Gift('Fancy Gift'), <T>(Gift t) {
print('${(t as Gift).name} was opened.');
});
or
The getter 'name' isn't defined for the type 'Object'
var present = Present<Gift>(Gift('Fancy Gift'), <Gift>(t) {
print('${t.name} was opened.');
});
Here is the working example with a cast.
void main() {
var present = Present<Gift>(Gift('Fancy Gift'), <T>(t) {
print('${(t as Gift).name} was opened.');
});
present.open();
}
class Present<T> {
final T _item;
final Function<T>(T t) openedCallback;
T open() {
openedCallback.call(_item);
return _item;
}
Present(this._item, this.openedCallback);
}
class Gift {
final String name;
Gift(this.name);
}
There should be a way to do this without a cast right?
Your class definition does not do what you intend:
class Present<T> {
final T _item;
final Function<T>(T t) openedCallback;
...
openedCallback is separately parameterized; its T type parameter is separate and independent from that of Present<T>. There is no need to parameterize openedCallback since you presumably want:
class Present<T> {
final T _item;
final Function(T t) openedCallback;
...
After that, you can do:
var present = Present<Gift>(Gift('Fancy Gift'), (t) {
print('${t.name} was opened.');
});
Note that doing <T>(t) { ... } or <Gift>(t) { ... } is counterproductive. That declares an anonymous function that itself is generic and is has a type parameter named T or Gift respectively.
For instance, in Javascript I can do something like:
class Foo {
x = 'baz';
bar() {
const someVar = 'x';
console.log(this[someVar]);
// Output: 'baz';
}
}
Hopefully that's relatively clear - it boils down to accessing a member variable by another variable's contents. How is this achieved in Dart?
This is not trivial in Dart. Dart doesn't have a syntax to access class properties with [].
There are a couple of approaches though:
Mirrors:
https://api.dartlang.org/stable/2.6.1/dart-mirrors/dart-mirrors-library.html
Basically you have access to everything and offers the biggest freedom. You can check what properties a class has, access them via names and so on. Big disadvantage is that the generated JS (if targeting web) will be huge. Flutter doesn't support it at all.
Reflectable
To deal with the large generated JS, you can use package:reflectable. Never tried it with Flutter. It's a bit more to set up and start using bit it works.
Dart only solution 1
You can overload [] operator on a class:
class Foo {
final _backing = <String, String>{
'foo': 'bar'
};
operator [](String val) {
return _backing[val];
}
}
void main() {
final inst = Foo();
print(inst['foo']);
}
Dart only solution 2
Just use a map :) Well sort of... If you are dealing with complex types and you want to add some extra functionality to your map, you can do something like this:
import 'dart:collection';
class StringMap extends Object with MapMixin<String, String> {
final _backing = <String, String>{};
#override
String operator [](Object key) {
return _backing[key];
}
#override
void operator []=(String key, String value) {
_backing[key] = value;
}
#override
void clear() {
_backing.clear();
}
#override
Iterable<String> get keys => _backing.keys;
#override
String remove(Object key) {
return _backing.remove(key);
}
}