-
-
Save xenoterracide/7cf9a94e953a7226b929 to your computer and use it in GitHub Desktop.
mixins In Perl6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mixins are called roles in perl 6 and also can behave as traits and interfaces | |
class ALogger { method debug( Str:D $message ) {...}} | |
class BLogger { method debug( Str:D $message ) {...}} | |
role Logger { | |
method log( Str:D $message ) { ... } | |
} | |
role LoggerInfo { | |
has $!log = ALogger.new(); | |
} | |
role LoggerDebug { | |
has $!log = BLogger.new(); | |
} | |
class MyLogger does Logger, LoggerDebug, LoggerInfo { | |
method log( Str:D $message ) { | |
$!log.debug($message); | |
} | |
} | |
class Handler { # Logger and UberLogger are valid | |
method logHelloWorld( Logger:D $log ) { | |
$log.log("hello world"); | |
# could call error, can't call trace (or maybe you can but shouldn't) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment