Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a type alias

Tags:

types

dart

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().

like image 747
user7610 Avatar asked Nov 03 '13 16:11

user7610


People also ask

What is a type alias?

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.

What is an alias in C++?

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.

Is Typedef just an alias?

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.

What is type alias in Elm?

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.


2 Answers

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);
}

EDIT 2.13

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

like image 174
atreeon Avatar answered Sep 28 '22 20:09

atreeon


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 = [];
}
like image 24
Alexandre Ardhuin Avatar answered Sep 28 '22 20:09

Alexandre Ardhuin