Clarifying Roles. Jonathan Worthington German Perl Workshop 2007

Similar documents
Programming II (CS300)

Programming II (CS300)

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Object-Oriented Concepts and Design Principles

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Polymorphism. Arizona State University 1

Idioms for Building Software Frameworks in AspectJ

Lecture 10. Overriding & Casting About

QUIZ. How could we disable the automatic creation of copyconstructors

Chapter 7. Inheritance

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Object oriented programming Concepts

Object-Oriented Programming

CS200: Advanced OO in Java

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Inheritance and Polymorphism

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Rules and syntax for inheritance. The boring stuff

COP 3330 Final Exam Review

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

Chapter 5 Object-Oriented Programming

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Programming Exercise 14: Inheritance and Polymorphism

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CPS 506 Comparative Programming Languages. Programming Language

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

QUIZ. How could we disable the automatic creation of copyconstructors

Object-Oriented Programming Paradigm

Inheritance (Chapter 7)

Traits: Tools and Methodology

What is Inheritance?

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Inheritance and Polymorphism in Java

Class Diagram. Classes consist of. Note that class diagrams contain only classes, not objects.

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

CS-202 Introduction to Object Oriented Programming

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

Chapter 4 Defining Classes I

Software Engineering

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Java for Non Majors Spring 2018

Class Diagram. Classes consist of. Note that class diagrams contain only classes, not objects.

BM214E Object Oriented Programming Lecture 11

More about inheritance

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

ITI Introduction to Computing II

CSE115 Introduction to Computer Science for Majors I Spring 2008 Final Exam. First/Given Name (PRINT) May 6, 2008

Scala. Fernando Medeiros Tomás Paim

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 10 Classes Continued. Fundamentals of Java

Object Orientated Analysis and Design. Benjamin Kenwright

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

COMP 110/L Lecture 19. Kyle Dewey

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

The Perl 6 Express Jonathan Worthington Belgian Perl Workshop 2009

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Object-Oriented Design

ITI Introduction to Computing II

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

CHAPTER. Knowledge Representation

Java Programming Lecture 7

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Lecture 13: more class, C++ memory management

Implementing Perl 6 Jonathan Worthington Dutch Perl Workshop 2008

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

ENCAPSULATION AND POLYMORPHISM

Traits in C# Stefan Reichhart Oscar Nierstrasz, Stephane Ducasse. Software Composition Group

The 10 Minute Guide to Object Oriented Programming

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

CS 11 java track: lecture 3

Inheritance. Exploring Polymorphism. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Software Design and Analysis for Engineers

Exploring Perl 6 Through Its Modules. Jonathan 6guts.wordpress.com

PROGRAMMING LANGUAGE 2

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

C++ Important Questions with Answers

Exam in TDDB84: Design Patterns,

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Inheritance & Polymorphism

Transcription:

Clarifying Roles Jonathan Worthington German Perl Workshop 2007

The Perl 6 Object Model The Perl 6 object model attempts to improve on the Perl 5 one Nicer, more declarative syntax One way to do things, rather than the many that appeared in Perl 5 (but you can still do other stuff if you like) Roles the subject of this talk Before roles, a look at classes

Classes In Perl 6 Introduce a class using the class keyword With a block: class Puppy { } Or without to declare that the rest of the file describes the class. class Puppy;

Attributes Introduced using the has keyword class Puppy { has $name; has $colour; has @paws; has $tail; } All attributes in Perl 6 are stored in an opaque data type Hidden to code outside of the class

Accessor Methods We want to allow outside access to some of the attributes Writing accessor methods is boring! $. means it is automatically generated class Puppy { has $.name; has $.colour; has @paws; has $tail; }

Mutator Methods We should be able to change some of the attributes Use is rw to generate a mutator method too class Puppy { has $.name is rw; has $.colour; has @paws; has $tail; }

Methods The new method keyword is used to introduce a method method bark() { say w00f! ; } Parameters go in a parameter list; the invocant is optional! method chew($item) { $item.damage++; }

Attributes In Methods Attributes can be accessed with the $. syntax, via their accessor method play_in_garden() { $.colour = 'black'; } To get at the actual storage location, $colour can be used method play_in_garden() { $colour = 'black'; }

Consuming A Class A default new method is generated for you that sets attributes Also note that -> has become. my $puppy = Puppy.new( name => 'Rosey', colour => 'white ); $puppy.bark(); # w00f! say $puppy.colour; # white $puppy.play_in_garden(); say $puppy.colour; # black

Inheritance A puppy is really a dog, so we want to implement a Dog class and have Puppy inherit from it Inheritance is achieved using the is keyword class Dog { } class Puppy is Dog { }

Multiple Inheritance Multiple inheritance is possible too; use multiple is statements class Puppy is Dog is Pet { }

In Search Of Greater Re-use In Perl 6, roles take on the task of reuse, leaving classes to deal with instance management We need to implement a walk method for our Dog class However, we want to re-use that in the Cat and Pony classes too What are our options?

The Java, C# Answer There s only single inheritance You can write an interface, which specifies that a class must implement a walk method Write a separate class that implements the walk method You can use delegation (hand coded) Sucks

The Multiple Inheritance Answer Write a separate class that implements the walk method Inherit from it to get the method Feels wrong linguistically A dog is a walk err, no A dog does walk what we want Multiple inheritance has issues

Multiple Inheritance Issues The diamond inheritance problem Do we get two copies of A s state? If B and C both have a walk method, which do we choose? B A D C Implementing multiple inheritance is tricky too

Mix-ins A mix-in is a group of one or more methods than can not be instantiated on their own We take a class and mix them in to it Essentially, these methods are added to the methods of that class Write a Walk mixin with the walk method, mix it in.

How Mix-ins Work Defined in terms of single inheritance C C M 1 M 2 M 1 M 2 C with M 1 and M 2 mixed in is, essentially, an anonymous subclass

Issues With Mix-ins If M 1 and M 2 both have methods of the same name, which one is chosen is dependent on the order that we mix in Fragile class hierarchies again Further, mix-ins end up overriding a method of that name in the class, so you can t decide which mix-in s method to actually call in the class itself

The Heart Of The Problem The common theme in our problems is the inheritance mechanism Need something else in addition We want To let the class be able to override any methods coming from elsewhere Explicit detection and resolution of conflicting methods

Flattening Composition A role, like a mix-in, is a group of methods If a class does a role, then it will have the methods from that role, however: If two roles provide the same method, it s an error, unless the class provides a method of that name Class methods override role methods

Creating Roles Roles are declared using the role keyword Methods declared just as in classes role Walk { method walk($num_steps) { for 1..$num_steps {.step for @paws; } } }

Composing Roles Into A Class Roles are composed into a class using the does keyword class Dog does Walk { } Can compose as many roles into a class as you want Conflict checking done at compile time Works? Not quite

Composing Roles Into A Class Notice this line in the walk method:.step for @paws; Can state that a role shares an attribute with the class it is composed into using has without. or! has @paws; Note: to use this currently in Pugs, you must use:.step for @!paws;

Parametric Polymorphism Polymorphism = code can work with values of different types Parametric = a type takes a parameter; we pass a type variable whenever we use the type What is the type of the invocant (self) for a method in a role? That of the class we compose it into

Parametric Polymorphism The types of roles are therefore parametric They are parameterised on the type of the class that we compose the role into Compose Walk into class Dog, the invocant has type Dog Compose Walk into class Cat, the invocant has type Cat

The End

w00f!

Questions?