Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Roles

Name: Anonymous 2013-09-06 10:16

Roles implement a bit more flexibility in the class-object programming model.
It allows to create "behavoir sets", that cannot be instantiated, but can be applied to other classes or objects.

role SharpShooter {
   method Shoot { say 'Shoot(Precision => 99)' };
   method See   { say 'See(HowFar => 50)' };
}
role Shooter {
   method Shoot { say 'Shoot(Precision => 60)' };
   method See   { say 'See(HowFar => 20)' };
}
role Thief {
   method Steal  { say 'Pickpocket( Smoothness => 50 )' };
   method Escape { say 'Run ( Speed => 50 )' };
}
role Mortal {
   has $.Age;
   method HappyBirthday {
      say 'Unit is One year older';
      $.Age += 1; 
   };
}
role Inmortal {
   has $.Age;
   method HappyBirthday { say 'Unit do not age at all' };
}
class NinjaElf does SharpShooter does Thief does Inmortal {
   method Taunt { say 'I will gladly give my inmortal life for my mission' };
}
class Archer does SharpShooter does Mortal {
   method Taunt { say 'Right between the eyes!' };
}
class ElfArcher is Archer does Inmortal {
   method Taunt { say 'I have been practicing my aiming for a thousand years.'}
}
my NinjaElf  $Unit1 .= new;
my Archer    $Unit2 .= new;
my ElfArcher $Unit3 .= new;
my @Units = $Unit1, $Unit2, $Unit3;
for (@Units) -> $Unit {
   print '*The unit says: ';
   $Unit.Taunt;
   say 'Watch your head'    if $Unit.^does(SharpShooter);
   say 'Watch your pockets' if $Unit.^does(Thief);
}

Output...

*The unit says: I will gladly give my inmortal life for my mission
Watch your head
Watch your pockets
*The unit says: Right between the eyes!
Watch your head
*The unit says: I have been practicing my aiming for a thousand years.
Watch your head

With roles, you can model complex behavoirs and its combinations, without having to stick to a rigid class heriarchy. Being that you can assign roles at runtime also, this allows to to create powerful dynamic objects that can change their behavoir on the fly.

$Unit2 does Thief; # Now our archer has Thief skills

Name: Anonymous 2013-09-06 10:29

>NinjaElf 
>Inmortal
Badly designed RPG by a script kiddie

Name: Anonymous 2013-09-06 12:23

>>2
You're anus is badly designed

Name: Anonymous 2013-09-06 16:57

Scala thought of it before you!
trait SharpShooter {
   def Shoot = say "Shoot(Precision => 99)"
   def See   = say "See(HowFar => 50)"
}
trait Shooter {
   def Shoot = say "Shoot(Precision => 60)"
   def See   = say "See(HowFar => 20)"
}
trait Thief {
   def Steal  = say "Pickpocket( Smoothness => 50 )"
   def Escape = say "Run ( Speed => 50 )"
}
trait Mortal {
   var Age: Int = 0
   def HappyBirthday = {
      say "Unit is One [sic] year older"
      Age += 1
   }
}
trait Inmortal {
   var Age: Int = 0
   def HappyBirthday = say "Unit do [sic] not age at all"
}
class NinjaElf extends SharpShooter with Thief, Inmortal {
   def Taunt = say "I will gladly give my inmortal life for my mission"
}
class Archer extends SharpShooter with Mortal {
   def Taunt = say "Right between the eyes!"
}
class ElfArcher extends Archer with Inmortal {
   def Taunt = say "I have been practicing my aiming for a thousand years."
}
val Unit1 = new NinjaElf
val Unit2 = new Archer
val Unit3 = new ElfArcher
val Units = List(Unit1, Unit2, Unit3)
for (Unit <- Units) {
   print "*The unit says: "
   Unit.Taunt
   unit match {
      case _: SharpShooter => say "Watch your head"
      case _: Thief => say "Watch your pockets"
   }
}

Name: Anonymous 2013-09-06 16:58

Forgot to "[sic]" the Inmortals. Oh well.

Name: Anonymous 2013-09-06 18:26

Scala is a privileged language
just like Perl6, only more mature.

Don't change these.
Name: Email:
Entire Thread Thread List