Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Classes Suck. ROLES are the new best thing

Name: Anonymous 2010-09-09 15:09

quote:

Roles in Perl 6 take on the function of interfaces in Java, mixins in Ruby, and traits[26] in the Smalltalk variant Squeak. These are much like classes, but are entirely abstract. These are used to perform composition when used with classes rather than adding to their inheritance chain. Roles define nominal types; they provide semantic names for collections of behavior and state. The fundamental difference between a role and a class is that classes are instantiable; roles are not.[27]

Essentially, a role is a bundle of (possibly abstract) methods and attributes that can be added to a class without using inheritance. A role can even be added to an individual object; in this case, Perl 6 will create an anonymous subclass, add the role to the subclass, and change the object's class to the anonymous subclass.

For example, a Dog is a Mammal. Dogs inherit certain characteristics from Mammals, such as mammary glands and (through Mammal's parent, Vertebrate) a backbone. Dogs may have one of several distinct types of behavior; for example, a Dog may be a Pet, a Stray, or a Guide for the blind. However, these are simply sets of additional behaviors that can be added to a Dog; a Cat can equally be a Pet or Stray, for example. Hence, Dog and Mammal are classes, while Pet, Stray, and Guide are roles.
 class Mammal is Vertebrate {
     . . .
 }
 class Dog is Mammal {
     . . .
 }
 role Pet {
     . . .
 }
 role Stray {
     . . .
 }
 role Guide {
     . . .
 }

Roles are added to a class or object with the does keyword, as opposed to inheritance's is. The keywords reflect the differing meanings of the two features: role composition gives a class the behavior of the role, but doesn't indicate that it is truly the same thing as the role.
 class GuideDog is Dog does Guide {
     . . .
 }   # Subclass composes role
 
 my $dog = new Dog;
 $dog does Guide;                         # Individual object composes role

Although roles are distinct from classes, both are types, so a role can appear in a variable declaration where one would normally put a class. For example, a Blind role for a Human could include an attribute of type Guide; this attribute could contain a Guide Dog, a Guide Horse, a Guide Human, or even a Guide Machine.
 class Human {
     has Dog $dog;                        # Can contain any kind of Dog, whether it does the
     ...                                  # Guide role or not
 }
 role Blind {
     has Guide $guide;                    # Can contain any object that does the Guide role,
     ...                                  # whether it is a Dog or something else
 }

Name: Anonymous 2010-09-10 21:31

>>40
Let's fix it with syntax! Then, if it compiles, we'll know it's right!

Name: Anonymous 2010-09-10 23:20

>>41
Semantics. And in Go's case, if it compiles... well that don't mean a thing. This, in the face of the draconian emphasis on type safety. It's not that bad unless someone slips up pretty bad... but then that's just what type safety is supposed to prevent.

Name: Anonymous 2010-09-10 23:47

>>42
Successful compilation has never meant a thing.

Name: Anonymous 2010-09-11 0:02

>>43
With regards to type checking in a statically typed language it is supposed to mean a lot. Or do you disagree?

Name: Anonymous 2010-09-11 5:09

>>44
>>44:1: error: expected 'sense of humour' before posting
>>44:2: error: Aspie detected: I'm pretty sure that was a joke.
make: *** [>>44] Error 1

Name: Anonymous 2010-09-11 6:41

So has anyone provided an example where Perl roles aren't a functional subset of classes, or could be with trivial extensions to classes?

Name: Anonymous 2010-09-11 6:43

>>44
I do, C is my proof.

Name: Anonymous 2010-09-11 8:31

Multiple inheritance, polymorphism, classes, and now roles, they're all just a bunch of enterprise-speak for keeping related blobs of data together and treating them as one entity with different possible operations.

Name: Anonymous 2010-09-11 13:29

>>46
``Roles" are just interfaces, which are a trivial extension to classes.

Name: Anonymous 2010-09-11 14:37

>>49
Your full of bullshit

Name: Anonymous 2010-09-11 15:11

>>45
Hardly. At least >>47 was funny.

Name: Anonymous 2010-09-12 5:06

>>1
Admit your role!

Name: Anonymous 2010-09-12 8:57

>>50
What about my full of bullshit?

Name: Anonymous 2010-09-12 9:04

The term `role' suggests a method may have different behavior with respect to different roles it can fulfill. `Interface' is a better term for indicating a set of features and behaviors.

Name: Anonymous 2010-09-12 9:10

Socialistic propaganda.

Name: Anonymous 2010-09-12 21:33

>>54
typeclasses

Name: Anonymous 2010-09-12 22:37

>>1
That's how I... role!

Name: Anonymous 2010-09-13 5:05

>>57
This isn't reddit, we don't do pun threads

Name: Anonymous 2010-09-13 6:29

>>58
ROLE on floor laughing

Name: Anonymous 2010-09-13 8:14

>>59
ROLE on land enjoying

Name: Anonymous 2010-09-13 12:08

role Barrel {
...
}

Name: Anonymous 2010-09-13 16:23

ROLE for anal circumference.

Name: Anonymous 2010-09-13 17:13

role PlayingGame {
        role (10) to continue;
}

Name: Anonymous 2010-12-17 1:33

Erika once told me that Xarn is a bad boyfriend

Name: Anonymous 2011-01-18 10:52

bampu pantsu

Name: Anonymous 2011-04-06 19:07

so that means that is easier to make a RPG in perl6?

Name: Anonymous 2011-04-06 20:06

class Haskell does Dog does Dead { ... }

Name: Anonymous 2011-04-06 20:07

>>68

[name='Haskell type='dog state='dead]

Name: Anonymous 2011-04-06 20:10


dog  ["Haskell"] = true;
dead ["Haskell"] = true;

Name: Anonymous 2011-04-06 20:12


connect haskell dog
connect necrosis haskell
synth necrosis

Name: Anonymous 2011-04-06 20:24

>>68
class Haskell is Dog but Dead.

Name: Anonymous 2011-04-06 20:26


synthCorrelation haskell dog dead

Name: Anonymous 2011-04-06 20:27


synthCorrelation [haskell=ye dog=ye dead=ye] -> So it goes.

Name: code less, hax my anus 2011-04-06 20:29

>>74
'(haskell (dog) (dead . #t))

Name: Anonymous 2011-04-06 20:30


synthCorrelation [hatedLanguage?=ye animal?=ye dead=ye] -> nice indeed.

Name: Anonymous 2011-04-06 20:32

synthCorrelation [perl=ye caml=ye dead=ye] -> nice indeed.

Name: Anonymous 2011-04-06 20:56

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to remove. -- Exupery

Name: Anonymous 2011-04-06 20:57

Perfection is achieved when there is nothing left to remove. -- Exupery
just applied the principle to quotation.

Name: Anonymous 2011-04-06 21:19

Hey this is pretty cool

my $dog = new Dog;
 $dog does fuckyouintheass;                         # Individual object composes role

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List