Classes, interfaces, and mixins: an analogy

jdavidb on 2008-12-15T23:03:34

A class is a noun: Ball.

An interface is an adjective, usually of the form verb-"able": Serializable. Shootable.

A mixin is a gerund: Serializing. Encrypting.

Too bad there's no mixins in the language I have to speak every day. :)

Warning: I've got no clue about this stuff, and I'm a learner expressing an analogy that occurred to him which is likely dead wrong.


Then what is a Role?

perigrin on 2008-12-16T04:01:08

because they can be either an interface

role Shootable {
        requires qw(ready aim fire);
}

or a Mixin

role Shooting {
        requires qw(ready);
        method aim (Object target) { ... }
        method fire () { ... }
}

or something else entirely …

role Gun with Shooting {
        has gun => (
                is => 'Gun',
                is => 'ro',
                handles => { load => 'ready' },
        );
}

Re:Then what is a Role?

Aristotle on 2008-12-16T22:41:35

Still an adjective. Think of interfaces as a flawed form of roles.

Re:Then what is a Role?

jdavidb on 2008-12-17T20:22:30

I thought a Role was a Mixin. :) But I'd say in this case, it depends on how you use it. Kind of like how an abstract class which implements no methods at all is still an interface, even though it might not be called one.

Re:Then what is a Role?

perigrin on 2008-12-17T22:24:47

Nah, Mixin inheritance isn't as safe as Role composition. Mixin's can cause diamond patterns in your inheritance, Roles can't (amongst other things).

Re:Then what is a Role?

jdavidb on 2008-12-18T16:08:28

I clearly still have a lot to learn, then. :)