In my studies of Dart languages, I found some packaged using this kind of class declaration:
class _Base = Authentication with Utilities, Validators;
I don't understand what the operator = is doing here, is it a kind of alias?
Is a shortcut to create inherit classes with mixins.
The example is same to:
class _Base extends Authentication with Utilities, Validators{
_Base(): super(); // or with args
}
Utilities and validators are mixins.
Related
For abstract classes is there difference between implements and extends? Which one should I use? In Java for interfaces you would use implements, but I see dart doesn't have interfaces and both implements/extends work. If I want to declare abstract class for my api methods, should I use implements or extends?
void main() {
User user = new User();
user.printName();
}
abstract class Profile {
String printName();
}
class User extends Profile {
#override
String printName() {
print("I work!!");
return "Test";
}
}
All classes in Dart can be used as interfaces. A class being abstract means you cannot make an instance of the class since some of its members might not be implemented.
extends means you take whatever a class already have of code and you are then building a class on top of this. So if you don't override a method, you get the method from the class you extends from. You can only extend from one class.
implements means you want to just take the interface of class but come with your own implementation of all members. So your class ends up being compatible with another class but does not come with any of the other class's implementation. You can implement multiple classes.
A third options, which you did not mention, is mixin which allow us to take the implementation of multiple mixin defined classes and put them into our own class. You can read more about them here: https://dart.dev/guides/language/language-tour#adding-features-to-a-class-mixins
Can anyone help me understand this piece of code?
#immutable
abstract class User with _$User {
const factory User(String name, int age) = _User;
}
The syntax I particularly want to understand is the benefit for the class to mixin itself apparently and what the prefix _$ means in the declaration.
From what I see you most likely dealing with class that will have more code generated using code generator.
Class User does not mixin itself. It mixins class $User. Class $User will be generated and placed in appropriate *.g.dart file which will be part of your *.dart file once code generator is complete.
How to create mixins for Polymer.AppLocalizeBehavior for Polymer 2.x?
I am using Redux mixins using following syntax:
class MyCreateAccount extends ReduxBehavior(Polymer.Element) {..}
I need to add Polymer.AppLocalizeBehavior to my class declaration and need help in creating the required mixin.
In the class-based syntax you can simulate element multiple inheritance of class mixins with something.
For example if you've created a redux store file like this
<...your redux store content...>
const store = Redux.createStore((state = {}, action) => state)
const ReduxMixin = PolymerRedux(store)
You can import that file and bind elements using redux mixin wrap the mixin behaviors which you can include your behaviors inside.
class MyCreateAccount extends ReduxMixin(Polymer.mixinBehaviors([Polymer.AppLocalizeBehavior], Polymer.Element)) { .. }
or you can use the Polymer 2 hybrid behaviours as mixins by extending Polymer.mixinBehaviors([behaviors], superclass) as below
class MyCreateAccount extends Polymer.mixinBehaviors([Polymer.AppLocalizeBehavior, ReduxBehavior], Polymer.Element) { .. }
Can someone explain me the difference between both interfaces InputFilterAwareInterface and InputFilterProviderInterface? Both seem to serve to the same purpose, to get an InputFilter, but I know they cannot be the same... And when do they get called?
Thanks
Both interfaces exist for different purposes. The InputFilterAwareInterface guarantees that implemented classes will have a setInputFilter() and getInputFilter() methods which accept and return an InputFilter instance when necessary. On the other hand, the InputFilterProviderInterface guarantees only that implemented classes will have a getInputFilterSpecification() method which returns a filter specification (configuration array) which is ready to use as argument in various input factories.
For example; the snippet below came from Zend\Form\Form.php class:
if ($fieldset === $this && $fieldset instanceof InputFilterProviderInterface) {
foreach ($fieldset->getInputFilterSpecification() as $name => $spec) {
$input = $inputFactory->createInput($spec);
$inputFilter->add($input, $name);
}
}
As you can see, the Form class creates inputs and binds them to related filter using given specification which is returned by getInputFilterSpecification() method of the implementing class ($fieldset int this case).
Using Traits
Zend Framework 2 also provides lot of traits for commonly used interfaces. For example InputFilterAwareTrait for InputFilterInterface. This means, you can easily implement that interface if you have PHP >= 5.4
namespace MyNamespace;
use Zend\InputFilter\InputFilterInterface;
MyClass implements InputFilterInterface {
// Here is the trait which provides set and getInputFilter methods
// with a protected $inputFilter attribute to all MyClass instances.
use \Zend\InputFilter\InputFilterAwareTrait;
// Your other methods.
...
}
Now anywhere in your code, you can do this:
$myClass->setInputFilter($AnInputFilterInstance);
$myClass->getInputFilter(); // Returns an inputfilter instance.
As you can imagine, no trait exists for InputFilterProviderInterface because its responsibility is only returning a valid config spec. It doesn't deal with any instance or class attribute like is forced in InputFilterInterface.
I have written a list() method for retrieving a list of domain class instances matching a set of filters, and this method is used for different domain classes ; the code is exactly the same except the class on which the GORM methods are called :
Store => Store.createCriteria()
Client => Client.createCriteria()
and so on.
To avoid code duplication, I have tried to make a generic version of the list method, by creating a generic class :
class QueryUtils<T> {
def list(params) {
T.createCriteria()
[...]
}
}
This way, each of my services (StoreService, ClientService, etc) can extend QueryUtils :
class StoreService extends QueryUtils<Store>
class ClientService extends QueryUtils<ClientService>
and so on, and inherit the list() method corresponding to its domain class type.
The problem is that during the execution, it doesn't work since the effective type of T is java.lang.Object, instead of the domain class type I have specified :
groovy.lang.MissingMethodException: No signature of method: static java.lang.Object.createCriteria() is applicable for argument types: () values: []
Do you know how to solve this problem?
I did something like this a while back for Hibernate (outside of Grails) - https://burtbeckwith.com/blog/?p=21
But it doesn't work with Groovy since the compiler ignores generics. But you could change it to take the class in the constructor instead of as a generic type:
class QueryUtils {
private final Class clazz
QueryUtils(Class clazz) {
this.clazz = clazz
}
def list(params) {
clazz.createCriteria()
[...]
}
}