What can I do to prevent the compiler from throwing the following warning
Missing concrete implementation of setter 'MyClass.field' and getter
'MyClass.field'
on the following code?
import 'package:mock/mock.dart';
class MyClass {
String field;
}
#proxy
class MockMyClass extends Mock implements MyClass{}
Implement noSuchMethod(); When a class has a noSuchMethod() it implements any method. I assume this applies to getter/setter as well because they are just special methods (never tried myself yet though). See also https://www.dartlang.org/articles/emulating-functions/#interactions-with-mirrors-and-nosuchmethod
The warning comes because you have a class that doesn't implement its interface, and which doesn't declare a noSuchMethod method.
It's not sufficient to inherit the method, you have to actually declare it in the class itself.
Just add:
noSuchMethod(Invocation i) => super.noSuchMethod(i);
That should turn off the warning for that class.
About this warning ( Dart Spec ) :
** It is a static warning if a concrete class does not have an implementation for a method in any of its superinterfaces unless it declares its own noSuchMethod method (10.10).
**
So you can create an implementation for getter field in your MockMyClass class.
Related
I couldn't understand the difference between these two keywords.
By using extends we can get features from parent class. I think implements does that too.
First I thought the difference is overriding methods but with extends I can do that.(I might be wrong)
Is the difference of these two keywords about overriding methods or what? Thank you
extends means we get the implementation of a given class and we can then override members if we want our own implementation for certain variables or methods. You can also add new variables and methods.
implements means you get nothing from the class you implement from. But you promise that your class will be compatible with the interface of the class you are implementing. So no, you are not getting any implementation from the super class and you need to implement everything or declare your class abstract.
Say I have the abstract class A
abstract class A {
A.someConstructor(Foo foo);
}
and all subclasses of A should then implement such constructor:
class B extends A {
#override
B.someConstructor(Foo foo) {
// ...
}
}
So basically what I want is some kind of abstract constructors.
Is there any way of achieving this (of course the above code does not work) or do I need a normal abstract method which then creates the object and sets its properties?
EDIT: Ok so it looks like the only way to create at least a similar behaviour would be something like this:
abstract class A {
A.someConstructor(Object foo);
}
class B extends A {
B.someConstructor(Object foo) : super.someConstructor(foo) {
// ...
}
}
This isn't exactly useful, and after some thinking about my problem I realized that in fact my original goal itself is not really neccessary, so this questions is now answered.
You want to enforce a pattern on the constructors of subclasses. The Dart language has no support for doing that.
Dart has types and interfaces which can be used to restrict values and class instance members.
If a class implements an interface, then its instance members must satisfy the signatures declared by the super-interface. This restricts instance members.
If a variable has a type, for example a function type, then you can only assign values of that type to it. This restricts values. Because a class is a subtype of its interfaces, the subclass restriction means that class typed variables can be used safely (the subtype can be used as its supertype because it has a compatible interface).
There is no way to restrict static members or constructors of classes, or members of libraries, because there is no way to abstract over them. You always have to refer directly to them by their precise name, so there is no need for them to match a particular pattern.
(Which may explain why you found the goal not necessary too).
In this situation, your subclasses must call the A.someConstructor constructor, but they are free to choose the signature of their own constructors. They can do:
class B extends A {
B.someConstructor(Object foo) : super.someConstructor(foo);
}
// or
class C extends A {
C.differentName(Object foo) : super.someConstructor(foo);
}
// or even
class D extends A {
D() : super.someConstructor(new Object());
}
Constructors aren’t inherited
Subclasses don’t inherit constructors from their superclass. A
subclass that declares no constructors has only the default (no
argument, no name) constructor.
Source
From here:
bool shouldReclip(covariant CustomClipper<T> oldClipper);
AFAIK, the keyword covariant is used when you override a method explicitly telling the analyzer that you're going to provide it a valid type. But in CustomClipper class, there's no such override, so why covariant is used?
You can mark a parameter as covariant in the superclass, then subclasses automatically get their parameters marked covariant too.
If the class is intended to be extended, this can be a service (and documentation) for the subclass authors so they know that the parameter is supposed to be used covariantly.
If you look at this subclass, you can see that they declare:
#override
bool shouldReclip(_DecorationClipper oldClipper) {...}
which is covariantly overriding the parameter type CustomClipper<Path> with _DeclarationClipper (which implements CustomClipper<Path>), and they don't have to write covariant here because the superclass declared it for them.
I was learning dart but it was still an error when I entered into the mixin, I don't know the fault where it is always an error when:
Abstract classes cannot be instantiated
I want to implement a mixin for Cat, Elang and Hiu with a subclass of Mamalia, Burung, Ikan
This is the Github code:
The error are rather clear. You cannot make an instance of an abstract class since an abstract class per definition is a class which can define methods and fields which are yet to be implemented and the class is therefore not complete.
The purpose of abstract classes is to let other classes extend from them and implement the missing methods.
I don't know why all your classes are marked as abstract but you can just remove the abstract keyword from the classes: Mamalia, Ikan and Burung and it should work since all of these classes are not needed to be abstract.
Abstract Class -
Use the abstract modifier to define an abstract class—a class that can’t be instantiated. Abstract classes are useful for defining interfaces, often with some implementation.
Don't instantiate Mamalia, Burung or Ikan classes. Instead instantiate Cat, Elang and Hiu.
Mamlia mamal = Cat(); // Will allow accessing methods defined only in Mamal class
Cat cat = Cat() // Will allow accessing all the methods defined in Cat class
In many languages if you try to instantiate abstract class you get compile time error.
In Dart however, you get warning while compiling and a runtime exception AbstractClassInstantiationError.
Why is that? Can someone provide an example, where it's reasonable to compile such code?
Dart tries to allow you to run your program while you are developing it.
That is why many things that are compile time errors in other languages are compile-time warnings and runtime errors in Dart. This includes "x is Foo" where Foo doesn't exist, type-annotating with a non-existing types, and calling constructors of partial (abstract) classes.
In short: because it's not a problem that prevents the program from being compiled (unlike a syntax error that might mean the rest of the file is interpreted wrongly), so there is no reason to stop you from running the code. Only if you hit the branch that actually depends on the problem will your program be stopped.
It appears that the answer is factory constructor in abstract class:
abstract class Foo {
factory Foo() { // make Foo appear to be instantiable
return new Bar();
}
some(); // some abstract method
Foo.name() {} just a named constructor
}
class Bar extends Foo {
Bar():super.name(); // call named super constructor
some() {} // implement abstract method
}
main() {
print(new Foo()); // "instantiate" abstract Foo
}
Output:
Instance of 'Bar'
Asking 'where it's reasonable to compile such code?' in Dart isn't very meaningful because Dart is an interpreted language - there is no compilation stage. Dart editors will issue warnings if they think you are making a type error as they analyse your code on the fly.
Factory constructors can be used to provide a 'default' concrete implementation of an abstract class as you have done in your example. This is used quite widely in Dart. For instance, if you create a new Map object you actually get a LinkedHashMap object - see this question and answer.
In your example your are not instantiating an instance of Foo ('new Foo' does not appear anywhere), you are instantiating a `Bar'. This is because when a factory constructor is called a new instance of its class is not automatically instantiated.