How do I give a new name to already existing type?
Let's say I want to give to a List<String>
the name Directives
and then be able to say directives = new Directives()
.
A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program. Type alias do not create new types. They simply provide a new name to an existing type.
You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is also referred to informally as a type alias). You can also use this mechanism to create an alias template, which can be useful for custom allocators.
typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.
A type alias is a shorter name for a type. For example, you could create a User alias like this: type alias User = { name : String , age : Int } Rather than writing the whole record type all the time, we can just say User instead.
It is possible now via an empty mixin
mixin ToAlias{}
class Stuff<T> {
T value;
Stuff(this.value);
}
class StuffInt = Stuff<int> with ToAlias;
main() {
var stuffInt = StuffInt(3);
}
It looks likely that unreleased 2.13 will contain proper type aliasing without the need of a mixin.
typedef IntList = List<int>;
IntList il = [1,2,3];
https://github.com/dart-lang/language/issues/65#issuecomment-809900008 https://medium.com/dartlang/announcing-dart-2-12-499a6e689c87
You can't define alias. For your case you can use DelegatingList from quiver to define Directives
:
import 'package:quiver/collection.dart';
class Directives extends DelegatingList<String> {
final List<String> delegate = [];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With