Skip to content

Instantly share code, notes, and snippets.

@duck8823
Last active November 5, 2016 18:31
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 duck8823/4ea3957b3a2ad88f41c56e4b55ea0563 to your computer and use it in GitHub Desktop.
Save duck8823/4ea3957b3a2ad88f41c56e4b55ea0563 to your computer and use it in GitHub Desktop.
Perl6でコンストラクタでプライベートフィールドに値を渡したい
my class Hoge {
has Str $!fuga;
submethod BUILD(:$!fuga) {
}
method fuga {
return $!fuga;
}
}
my $hoge = Hoge.new(fuga => 'foo');
say $hoge.fuga; # foo
my class Hoge {
has Str $.fuga;
method fuga {
die 'not accessible.';
}
method fugafuga {
return $!fuga;
}
}
my $hoge = Hoge.new(fuga => 'foo');
say $hoge.fugafuga; # foo
say $hoge.fuga; # not accessible.
my class Hoge {
has Str $!fuga;
method fuga {
return $!fuga;
}
}
my $hoge = Hoge.new(fuga => 'foo');
say $hoge.fuga; # (Str)
my class Hoge {
has Str $!fuga;
method new(:$fuga) {
return self.bless(:$fuga);
}
method fuga {
return $!fuga;
}
}
my $hoge = Hoge.new(fuga => 'foo');
say $hoge.fuga; # (Str)
my class Hoge {
has Str $!fuga;
method new(:$fuga) {
my $self = self.bless;
$self!fuga($fuga);
return $self;
}
method fuga {
return $!fuga;
}
method !fuga($fuga) {
$!fuga = $fuga;
}
}
my $hoge = Hoge.new(fuga => 'foo');
say $hoge.fuga; # foo
say $hoge!fuga('bar'); # syntax error
my class Hoge {
has Str $.fuga;
}
my $hoge = Hoge.new(fuga => 'foo');
say $hoge.fuga; # foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment