September 2017
Beginner
402 pages
9h 52m
English
An object of the ModernHouse type is also a House. Consider a function f that takes the argument and thinks that it is an object of the House type. The signature of the function applies a restriction to the argument:
sub f(House $h) { say "There are {$h.rooms} rooms in this house.";}
Now, let's create two different houses and call the function with them:
my $house = House.new(rooms => 2);my $modern_house = ModernHouse.new(rooms => 3, power => 100);f($house);f($modern_house);
This code works perfectly and prints the expected strings:
There are 2 rooms in this house.There are 3 rooms in this house.
The function is only using the attribute, which is defined in the parent class, so it ...
Read now
Unlock full access