How to use class as interface

55 views
Skip to first unread message

hanguokai

unread,
Nov 23, 2011, 1:11:41 PM11/23/11
to General Dart Discussion
"The interface of class C is an implicit interface that declares
instance members" in spec.
This is very interesting that class can be seen as interface.

What are the scenarios we can use ?
What does this means a class implement another class or an interface
implement a class?
I think in most cases, we should extends a class instead of implement
a class.

Dart is single-inheritance and multi-implement. But if class seen as
interface, we can implement
multi class:
class A
class B
class C implements A, B

Gilad Bracha

unread,
Nov 23, 2011, 1:29:37 PM11/23/11
to hanguokai, General Dart Discussion
Hi,

This feature isn't implemented yet, but the idea is that you can avoid having to define both an interface and a class. If you use a class in an implements clause, you will be implementing the interface implicitly defined bu that class, but not inheriting from it. So you will not get the field layout, you won't inherit the methods etc. You are just declaring that your new class will conform to the interface of that class.

class Point {
  var x, y;
}

class PolarPoint implements Point {
  var rho, theta;
  get x ...
  get y ...
}

new PolarPoint() is Point; // true

PolarPoint does not have extra storage for x and y variables, but it emulates the protocol of a Point and is recognized as such by the system.
--
Cheers, Gilad

Rémi Forax

unread,
Nov 23, 2011, 2:24:21 PM11/23/11
to mi...@dartlang.org
On 11/23/2011 07:29 PM, Gilad Bracha wrote:
Hi,

This feature isn't implemented yet, but the idea is that you can avoid having to define both an interface and a class. If you use a class in an implements clause, you will be implementing the interface implicitly defined bu that class, but not inheriting from it. So you will not get the field layout, you won't inherit the methods etc. You are just declaring that your new class will conform to the interface of that class.

class Point {
  var x, y;
}

class PolarPoint implements Point {
  var rho, theta;
  get x ...
  get y ...
}

new PolarPoint() is Point; // true

PolarPoint does not have extra storage for x and y variables, but it emulates the protocol of a Point and is recognized as such by the system.

Very cool idea.
In that case, is there still a need for a keyword interface.

cheers,
Rémi

Gilad Bracha

unread,
Nov 23, 2011, 2:55:18 PM11/23/11
to Rémi Forax, mi...@dartlang.org
On Wed, Nov 23, 2011 at 11:24 AM, Rémi Forax <fo...@univ-mlv.fr> wrote:

Very cool idea.
In that case, is there still a need for a keyword interface.

cheers,
Rémi


Let's not run before we can walk.  We'll implement this, experiment, see the feedback etc.
 
--
Cheers, Gilad

Sean Eagan

unread,
Nov 23, 2011, 3:44:34 PM11/23/11
to Gilad Bracha, hanguokai, General Dart Discussion
I assume classes used as interfaces are allowed to have static members, but that those members are not included in the interface exposed by the class?  That is a bit confusing though, because:

* Normal interfaces are not allowed to have static members
* One might assume that all the members (including the static ones) of the class are included in the interface exposed by the class.
--
Sean Eagan

Peter Ahé

unread,
Nov 23, 2011, 4:13:23 PM11/23/11
to Sean Eagan, Gilad Bracha, hanguokai, General Dart Discussion
Hi Sean,

Can you provide an example of how static members would lead to confusion with the implicit interface of a class?

Cheers,
Peter

Sean Eagan

unread,
Nov 29, 2011, 9:33:34 AM11/29/11
to Peter Ahé, Gilad Bracha, hanguokai, General Dart Discussion
On Wed, Nov 23, 2011 at 3:13 PM, Peter Ahé <a...@google.com> wrote:
Hi Sean,

Can you provide an example of how static members would lead to confusion with the implicit interface of a class?


Say you have a class which only has static members.  A better way to do this would be to just make everything top-level in the library, and not even have a class, but since dart supports static members, and since it's the java way, it will be done:

class Utils {
  static String x = "x";
  static final int y = 1;

  static int    foo(     ) {/*...*/}
  static String bar(int x) {/*...*/}
}

The implicit interface of this class is empty, however, it will not appear that way to many.  Someone will try to provide an alternate implementation of this class:

class AltUtils implements Utils {
  static String x = "alt";
  static final int y = 2;

  static int    foo(     ) {/*...*/}
  static String bar(int x) {/*...*/}
}

They think the "implements Utils" means something, but it actually means nothing, and they will expect an error if Utils ever changes, but this error will never appear.

Thanks,
Sean Eagan

Dominic Hamon

unread,
Nov 29, 2011, 10:37:45 AM11/29/11
to Sean Eagan, Peter Ahé, Gilad Bracha, hanguokai, General Dart Discussion
On Tue, Nov 29, 2011 at 6:33 AM, Sean Eagan <seane...@gmail.com> wrote:

On Wed, Nov 23, 2011 at 3:13 PM, Peter Ahé <a...@google.com> wrote:
Hi Sean,

Can you provide an example of how static members would lead to confusion with the implicit interface of a class?


Say you have a class which only has static members.  A better way to do this would be to just make everything top-level in the library, and not even have a class, but since dart supports static members, and since it's the java way, it will be done:

class Utils {
  static String x = "x";
  static final int y = 1;

  static int    foo(     ) {/*...*/}
  static String bar(int x) {/*...*/}
}

Maybe this pattern should be discouraged by introducing the notion of a static class (or namespace to some):

static class Utils {
  String x = 'x';
  ...
}

in which all members are static, you cannot instantiate the class, and you cannot inherit from the class.
Reply all
Reply to author
Forward
0 new messages