Peter StJ
2015-07-14 15:34:36 UTC
I am trying to understand how to use factory constructor to return subtypes
where needed, but I keep getting it wrong.
The main idea is simple: have a base class that defines the interface and
use factory to return subtypes. If needed in user code check the exact
type, otherwise use as usual. The reason to want template in the base class
is to provide information about the type of value to be returned.
Example:
class A<T> {
final T value;
A.internal(this.value);
factory A(value) {
if (value is String) return new A.internal(value);
else return new B(1);
}
}
class B extends A<int> {
B(value): super.internal(value);
}
The problem is reported issue: Return type 'B' is not a 'A<T>' as defined
by method A.create;
I don't see how it is not, when it is extending it and providing specific
type for 'T'.
I am doing something wrong when it comes to templetizing the whole thing.
The usage code would have been to have 'is' checks for the exact type and
then decide what to do with the 'value'. The reason I do not use dynamic
for value is that it can be 3 'types' of 'String' (i.e. depending on
formatting) and comparing it at each use is tedious, instead I want to use
the factory to return exact subtype that the user code knows how to handle
(or skip if does not) and still receive 'String' as type for 'value'.
Another use is to have the 'A' extended with template type set to something
entirely different (example usage would be 'content -> value to be the
content of a html.Node - it could be String, Node or NodeList).
Can you direct me in the right direction?
Thanks!
where needed, but I keep getting it wrong.
The main idea is simple: have a base class that defines the interface and
use factory to return subtypes. If needed in user code check the exact
type, otherwise use as usual. The reason to want template in the base class
is to provide information about the type of value to be returned.
Example:
class A<T> {
final T value;
A.internal(this.value);
factory A(value) {
if (value is String) return new A.internal(value);
else return new B(1);
}
}
class B extends A<int> {
B(value): super.internal(value);
}
The problem is reported issue: Return type 'B' is not a 'A<T>' as defined
by method A.create;
I don't see how it is not, when it is extending it and providing specific
type for 'T'.
I am doing something wrong when it comes to templetizing the whole thing.
The usage code would have been to have 'is' checks for the exact type and
then decide what to do with the 'value'. The reason I do not use dynamic
for value is that it can be 3 'types' of 'String' (i.e. depending on
formatting) and comparing it at each use is tedious, instead I want to use
the factory to return exact subtype that the user code knows how to handle
(or skip if does not) and still receive 'String' as type for 'value'.
Another use is to have the 'A' extended with template type set to something
entirely different (example usage would be 'content -> value to be the
content of a html.Node - it could be String, Node or NodeList).
Can you direct me in the right direction?
Thanks!
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.