Last active
November 5, 2016 18:31
-
-
Save duck8823/4ea3957b3a2ad88f41c56e4b55ea0563 to your computer and use it in GitHub Desktop.
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
my class Hoge { | |
has Str $!fuga; | |
submethod BUILD(:$!fuga) { | |
} | |
method fuga { | |
return $!fuga; | |
} | |
} | |
my $hoge = Hoge.new(fuga => 'foo'); | |
say $hoge.fuga; # foo |
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
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. |
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
my class Hoge { | |
has Str $!fuga; | |
method fuga { | |
return $!fuga; | |
} | |
} | |
my $hoge = Hoge.new(fuga => 'foo'); | |
say $hoge.fuga; # (Str) |
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
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) |
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
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 |
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
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