views:

198

answers:

3

What is the difference between:

type IFooable =
    interface IDisposable

    abstract Foo : (unit -> unit)

and

type IFooable =
    inherit IDisposable

    abstract Foo : (unit -> unit)

?

If equivalent, in which cases should I use one over the other ? Thanks!

A: 

u should use interfaces if you are only going to use abstract methods. Because you can only inherit one class but you can implement many interfaces.

L Digital Dash
You might not receive downvotes if you spell out your words.
recursive
+6  A: 

(I originally thought that) You must use 'inherit' with (at most one) base class. You can use 'interface' for any interfaces you are going to implement.

So in the case of IDisposable, it must be 'interface'.

EDIT, ok the compiler allows it, but that might be a bug, I'll look

EDIT: turns out it's a likely bug the other way, and likely interfaces will force you to use 'inherit' to inherit other interfaces, the idea being that 'inherited' members are always directly visible in the 'implicit interface' sense, whereas an 'interface' declaration on a class is an 'explicit' interface that requires a cast to that interface type to use those members. One way or another, we're likely to remove this flexibility in the language syntax so that there is only one way to author this, rather than two equivalent ways.

Brian
Great, thank you Brian.
Stringer Bell
Makes sense. I have a better understanding of this now. Thanks a lot!
Stringer Bell
A: 

I tend to use inheritance for true oop (i.e. X isa Y) and interfaces as a sort of decorator implementation.

probably not the defacto "proper" way to do it, but we quite like it.

what I mean is X will inherit Y, but then we'll want to give it some extra functionality from V, W, and Z.

"Jetta isa Car, but is also IDiesel, IGerman, IBroken"

this is just the way my current project does it, and I'll probably get downvoted for "abuse of oop", or something :)

Oren Mazor
Sounds crazy. What if your car implements both IBroken and IWorkingOrder? IToyota and IFord ?
Stringer Bell
okay so that was a terrible example! :)in reality, a better example would be a TabControl that has a bunch of specialized TabPages. some are ITxRx because they have certain types of behavior. others are another kind.crazy design, for sure, but oddly enough it works really well.
Oren Mazor