Jump to content

Sealed classes and methods: Difference between revisions

added Raku programming solution
m (Fixed typo.)
(added Raku programming solution)
Line 368:
<!--</syntaxhighlight>-->
Note however in Phix there is no possibility of invoking parent.watch_movie() from the else branch, because you have completely replaced that routine in the child instance. Should you want to share code in that kind of fashion you would need to give it a different and completely unambiguous name.
 
=={{header|Raku}}==
Raku doesn't have final class but [https://docs.raku.org/syntax/role Roles] is normally used to mimic the goal.
<syntaxhighlight lang="raku" line># 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 }</syntaxhighlight>
 
You may [https://ato.pxeger.com/run?1=nVPBToNAED144ysmxKRtgpyNPVo9adLYg_G4wlBQ2Gl2FxvS8CVeetCf8uKvuLtAoIVIldNm9r15bx6z7x-Cveb7_WeuoovLr7NvQSnCPb0l-MhUEKN4MIUdxEzCSgk49znLcO4AQIYqphAszjKmMw2UrAC3QkEiYWtuE74GFSNkBuVqatnh3zC1pE1Agg_xkb9Q0fA3Fc7V_NJxgpRJeeAVQkI54L6jxnE7tXMYAauHaeQ_pyjl9KoudrovmUCuDjSSI81_dbcxX8dJGv7qdiQbpmwyRJDlQTwYT0-j599mNm7lb6M9Uc7X402Pdsfc6M-OuSIhCq_eNw8KykH_CztsYZrrU7Vc7Wb57ty26Iw_7MNk0C_aIE40fnIaEYmBJfINf7IgztJwMvPs3D3RCnSXSNZAhs1VuFuBupWzA78NFRgPwW-XCMrqndfPvXn2Pw Attempt This Online!]
 
=={{header|Wren}}==
354

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.