I created an abstract class like this :
abstract class IRepository<T> {
}
After that I created an extension to this class :
extension Find<T> on IRepository<T>{
T get find => .....;
}
Now in other class, I try to use this extension like this: IRepository.find but I got this error The getter 'find' isn't defined for the type 'IRepository'.?
I imported locally my extension class for sure but I still have this error ?
You need to instantiate an object first and then try to use the find extensions. But abstract classes can't be instantiated. So you need to create some implementation first.
abstract class IRepository<T> {
}
class RepImpl extends IRepository<String>{
}
extension Find<T> on RepImpl{
String get find => ...;
}
and Then Create an object from RepImpl and you can access find getter.
Related
I have an abstract Dart class which contains some abstract methods and a concrete, implemented method that I want to keep consistent across all subclasses.
IntelliJ's Dart Analysis system is reporting in subclasses that no concrete implementation exists for my implemented method — even though I can see that abstract has a concrete implementation.
I have also tried implementing the method in my subclass and calling the super class, but that is also reported as not being implemented. Both abstract class and child class are in the same file.
abstract class Exporter {
List<String> getHeaderRow();
List<dynamic> getEntryAsRow(EntriesData entry);
/// Concrete implementation
dynamic getVal(dynamic value) {
return null;
}
}
// ExpenseReport underlined in red: missing concrete implementation of getVal
class ExpenseReport implements Exporter {
List<String> getHeaderRow() {
return [];
}
List<dynamic> getEntryAsRow(EntriesData entry) {
return [];
}
// dynamic getVal(dynamic value) {
// super.getVal(value); // IntelliJ reports "getval" as abstract in superclass
// }
}
Saying
class ExpenseReport implements Exporter
you mean your ExpenseReport implements interface of Exporter, which in turn means that it has to implement all methods declared in Exporter.
If you want to inherit implemented methods from abstract Exporter you need to extend it like this:
class ExpenseReport extends Exporter
I'm able to do something like the following in TypeScript
class Foo {
private constructor () {}
}
so this constructor is accessible only from inside the class itself.
How to achieve the same functionality in Dart?
Just create a named constructor that starts with _
class Foo {
Foo._() {}
}
then the constructor Foo._() will be accessible only from its class (and library).
A method without any code must be something like this
class Foo {
Foo._();
}
Yes, It is possible, wanna add more information around it.
A constructor can be made private by using (_) underscore operator which means private in dart.
So a class can be declared as
class Foo {
Foo._() {}
}
so now, The class Foo doesn't have a default constructor
Foo foo = Foo(); // It will give compile time error
The same theory applied while extending class also, It's also impossible to call the private constructor if it declares in a separate file.
class FooBar extends Foo {
FooBar() : super._(); // This will give compile time error.
}
But both above functionality works if we use them in the same class or file respectively.
Foo foo = Foo._(); // It will work as calling from the same class
and
class FooBar extends Foo {
FooBar() : super._(); // This will work as both Foo and FooBar are declared in same file.
}
you can create following class in order to get a singleton instance
class Sample{
factory Sample() => _this ??= Sample._();
Sample._(); // you can add your custom code here
static Sample _this;
}
Now in the main function you can call the sample constructor
void main(){
/// this will return the _this instace from sample class
Sample sample = Sample();
}
just use abstract class.
Because you can't instantiate abstract class
I want to encapsulate a generic object in another class without setting the generic type argument. I created a base Animal<T> class and defined other subclasses from it. Example:
public class Animal<T: YummyObject> {
// Code
}
public class Dog: Animal<Bark> {
// Code
}
public class Cat: Animal<Meow> {
// Code
}
and defined an Animal property, without the type argument, in the UITableView extension bellow:
extension UITableView {
private static var animal: Animal!
func addAnimal(animal: Animal) {
UITableView.animal = animal
}
}
but I get the following compile error when doing so:
Reference to generic type Animal requires arguments in <...>.
This seems to work fine in Java. How can I accomplish the same thing in Swift as well?
Swift doesn’t yet support wildcard-style generics like Java does (i.e., Animal<?>). As such, a common pattern is to define a type-erased superclass, protocol (or wrapper) to enable such usage instead. For instance:
public class AnyAnimal {
/* non-generic methods */
}
and then use it as your superclass:
public class Animal<T: YummyObject>: AnyAnimal {
...
}
Finally, use AnyAnimal in your non-generic code instead:
private static var animal: AnyAnimal!
Examples in the Swift Standard Library. For a practical example, see the KeyPath, PartialKeyPath, and AnyKeyPath classes hierarchy. They follow the same pattern I outlined above. The Collections framework provides even further type-erasing examples, but using wrappers instead.
I have a class I want to use mostly as a base class for other classes that have constant instances, but also as a mixin class for others. Ideally, I'm after something like the following:
class Base{
someMethod(){
//do something
}
}
class ConstantClass extends Base{
const ConstantClass();
anotherMethod(){
//do something else
}
}
class MixedClass extends NonMixinClass with Base{
thirdMethod(){
//a third thing
}
}
The above has an error in that ConstantClass cannot have a constant constructor without calling a super-class constant constructor. However, if I add a constructor of any kind to Base(), it can't be used as a mixin.
My current workaround is to duplicate functionality in static methods, as in the following:
class Base{
const Base();
static someStaticMethod(Base base){
//do something
}
someMethod() => Base.someStaticMethod(this);
}
class ConstantClass extends Base{
const ConstantClass(): super();
anotherMethod(){
//do something else
}
}
class BaseMixin implements Base{
someMethod() => Base.someStaticMethod(this);
}
class MixedClass extends NonMixinClass with BaseMixin{
thirdMethod(){
//a third thing
}
}
This isn't too bad when there's only one function in the base class, but things get pretty verbose for a complex class, and if there's a simple way of getting around the problem I'd love to keep things clean. Thanks in advance.
I though class ConstantClass extends Object with Base { would do it, but the new error I get is "Constant constructor cannot be declared for a class with a mixin". So I don't see a solution here, but not using a const constructor.
Many methods like complete in class Completer are marked "abstract", but in fact It can be directly invoked without being implemented. I'm really confused. Could anyone help me?
Yes, this can be a bit confusing. While abstract classes cannot be instantiated, it is possible to make them appear to be instantiable by defining a factory constructor. This is what Completer, Future and other abstract classes do:
abstract class Completer<T> {
factory Completer() => new _CompleterImpl<T>();
...
}
You can then invoke methods on the object created by the factory constructor. In the example above, factory Completer() returns a new _CompleterImpl object. Look at the (truncated) code of that class:
class _CompleterImpl<T> implements Completer<T> {
final _FutureImpl<T> _futureImpl;
_CompleterImpl() : _futureImpl = new _FutureImpl() {}
Future<T> get future {
return _futureImpl;
}
void complete(T value) {
_futureImpl._setValue(value);
}
...
}
and you see complete(); that is the method being invoked.