Skip to content

Instantly share code, notes, and snippets.

@Tux
Created February 18, 2015 11:45
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 Tux/ac2055cfd4b0806a538b to your computer and use it in GitHub Desktop.
Save Tux/ac2055cfd4b0806a538b to your computer and use it in GitHub Desktop.
use v6;
use Slang::Tuxic;
class C {
has Str $!foo = "A";
has Str $!bar = "B";
has Str $!baz = "C"; # Cannot be controlled from outside of class
method foo (Str $s) { $!foo = $s; }
method bar (Str $s) { $!bar = $s; }
submethod BUILD (*%init) {
for keys %init -> $attr {
my @can = self.can (lc $attr) or die "Unsupported attribute: $attr";
.(self, %init{$attr}) for @can;
}
self!sanity_check;
}
method !sanity_check {
$!foo eq $!bar and die;
}
}
C.new (foo => "D");
#C.new (box => "A"); # crash
C.new (bar => "A"); # crash
=>
use of uninitialized value $!bar of type Str in string context in method sanity_check at t.pl:22
use of uninitialized value $!foo of type Str in string context in method sanity_check at t.pl:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment