Skip to content

Instantly share code, notes, and snippets.

@xenoterracide
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenoterracide/7cf9a94e953a7226b929 to your computer and use it in GitHub Desktop.
Save xenoterracide/7cf9a94e953a7226b929 to your computer and use it in GitHub Desktop.
mixins In Perl6
# 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