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
}
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
}