Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Raku (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Inheritance, Roles and Classes==== Inheritance is the technique by which an object or type can re-use code or definitions from existing objects or types. For example, a programmer may want to have a standard type but with an extra attribute. Inheritance in other languages, such as Java, is provided by allowing Classes to be sub-classes of existing classes. Raku provides for inheritance via Classes, which are similar to Classes in other languages, and Roles. Roles in Raku take on the function of [[Interface (Java)|''interfaces'' in Java]], ''[[mixin]]s'' in Ruby, and ''[[Trait (computer science)|traits]]''<ref>{{cite web | title=Traits | url=http://www.iam.unibe.ch/~scg/Research/Traits/ | author=The Software Composition Group | year=2003 | access-date=22 September 2006 | archive-url=https://web.archive.org/web/20060811170712/http://www.iam.unibe.ch/~scg/Research/Traits/ | archive-date=11 August 2006 | url-status=dead }}</ref> in [[PHP]] and in the [[Smalltalk]] variant [[Squeak]]. These are much like classes, but they provide a safer composition mechanism.<ref>{{cite web| title=Day 18: Roles| url=https://perl6advent.wordpress.com/2009/12/18/day-18-roles/ | year=2009 | first=Jonathan| last=Worthington}}</ref> These are used to perform composition when used with classes rather than adding to their [[Inheritance (object-oriented programming)|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 can be instantiated; roles are not.<ref>{{cite web | title=The Why of Perl Roles | url=http://www.modernperlbooks.com/mt/2009/04/the-why-of-perl-roles.html | author=chromatic | year=2009 | author-link=chromatic (programmer)}}</ref> Although Roles are distinct from Classes, it is possible to write Raku code that directly instantiates a Role or uses a Role as a type object, Raku will automatically create a class with the same name as the role, making it possible to transparently use a role as if it were a class.<ref>{{Cite web|url=https://docs.raku.org/language/objects#Automatic_role_punning|title=Object orientation|website=docs.raku.org|access-date=2019-10-24}}</ref> 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, Raku 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]] because dogs inherit certain characteristics from Mammals, such as [[mammary gland]]s and (through Mammal's parent, [[Vertebrate]]) a [[Vertebral column|backbone]]. On the other hand, dogs also may have one of several distinct types of behavior, and these behaviours may change over time. For example, a Dog may be a [[Pet]], a [[feral|Stray]] (an abandoned pet will acquire behaviours to survive not associated with a pet), or a [[guide dog|Guide]] for the blind (guide dogs are trained, so they do not start life as guide dogs). However, these are sets of additional behaviors that can be added to a Dog. It is also possible to describe these behaviors in such a way that they can be usefully applied to other animals, for example, a [[Cat]] can equally be a Pet or Stray. Hence, Dog and Cat are distinct from each other, while both remain within the more general category Mammal. So Mammal is a Class and Dog and Cat are classes that inherit from Mammal. But the behaviours associated with Pet, Stray, and Guide are Roles that can be added to Classes, or objects instantiated from Classes. <syntaxhighlight lang="raku"> class Mammal is Vertebrate { ... } class Dog is Mammal { ... } role Pet { ... } role Stray { ... } role Guide { ... } </syntaxhighlight> Roles are added to a class or object with the <code>does</code> keyword. In order to show inheritance from a class, there is a different keyword <code>is</code>. 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. <syntaxhighlight lang="raku"> class GuideDog is Dog does Guide { ... } # Subclass composes role my $dog = new Dog; $dog does Guide; # Individual object composes role </syntaxhighlight> 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|Guide Horse]], a Guide Human, or even a Guide Machine. <syntaxhighlight lang="raku"> 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 } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)