Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can Dart's built-in List interface be instantiated?

Tags:

interface

dart

Notice: This question is obsolete; the interface declaration syntax has been removed from Dart:

  • Proposal to eliminate interface declarations from Dart
    "In Dart, every class engenders an implicit interface. Now that this feature is implemented, it is possible to actually eliminate interface declarations from the language. Interface declarations are replaced by purely abstract classes."

  • Issue 3972: Remove interface declaration support in VM


As far as I've been able to tell, it's impossible to instantiate an interface in Dart. If I simply try to construct a new MyInterface(), with or without a constructor defined, I get a run-time error (try it):

NoSuchMethodException - receiver: '' function name: 'MyInterface$$Factory' arguments: []]
interface MyInterface {}      
interface MyInterface {
  MyInterface();
}

If I try to use a factory constructor instead, returning an instance of an implementation, I get a compile-time error (try it):

SyntaxError: factory members are not allowed in interfaces
class MyImplementation implements MyInterface {
  MyImplementation();
 }

interface MyInterface {
  factory MyInterface() { return new MyImplementation(); }
}

However, this seems at-odds with the reality that List<E>1 in Dart's core library is an interface2, yet it has several constructors and can be instantiated. For example, this works fine (try it):

main() {
  var list = new List();
  list.add(5);
  print(list.last());
}

Why can List and many other built-in interfaces be instantiated? Is there some method I missed or are they just receiving special treatment as built-in types?


1Dart: Libraries: corelib: interface List<E>
2 "Much of the Dart Core Library is defined in terms of interfaces."3
3Dart: Tutorial: Interfaces

like image 671
Jeremy Avatar asked Oct 11 '11 07:10

Jeremy


1 Answers

The syntax for defining an interface is:

interfaceDefinition:
    interface identifier typeParameters? superinterfaces? factorySpecification? `{' (interfaceMemberDefinition)* `}'

Notice that the factorySpecification must come before the body of the interface rather than inside it.

So this is how you write it:

interface MyInterface default MyImplementation {

}

class MyImplementation implements MyInterface {
  MyImplementation();
}

Or if you want to go for the full generic definition:

interface MyInterface<E> default MyImplementation<E> {
    MyInterface(x);
}

class MyImplementation<E> implements MyInterface<E> {
  MyImplementation(this.x);
  E x;
}

Edit: For a more complete example, you can read the source code for the interface List<E> at https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/corelib/src/list.dart and the associated class ListFactory<T> source is at https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/runtime/lib/array.dart

like image 197
Duncan Avatar answered Dec 15 '22 19:12

Duncan