Sealed classes and methods

Raku doesn't have final class but Roles is normally used to mimic the goal.

# 20240626 Raku programming solution

role MovieWatcherRole { has Str $.name;
   method WatchMovie() { say "$.name is watching the movie"   }
   method EatPopcorn() { say "$.name is enjoying the popcorn" }
}

class MovieWatcher does MovieWatcherRole {
   method new(Str $name) { self.bless(:$name) }
}

class ParentMovieWatcher is MovieWatcher {
   method new(Str $name) { self.bless(:$name) }
}

role ChildMovieWatcherRole {
   method EatPopcorn() { say "$.name is eating too much popcorn" }
}

class ChildMovieWatcher is MovieWatcher does ChildMovieWatcherRole {
   method new(Str $name) { self.bless(:$name) }
}

role YoungChildMovieWatcherRole {
   method WatchMovie() { 
      say "Sorry, $.name, you are too young to watch the movie."; 
   }
}

class YoungChildMovieWatcher is ChildMovieWatcher does YoungChildMovieWatcherRole {
   method new(Str $name) { self.bless(:$name) }
}

for ParentMovieWatcher.new('Donald'),
    ChildMovieWatcher.new('Lisa'),
    YoungChildMovieWatcher.new('Fred')
{ .WatchMovie and .EatPopcorn }

You may Attempt This Online!

Last updated