raku-type-submethod
class Submethod is Routine {}
Submethod는 자식 클래스에 상속되지 않는 메서드예요. 주로 클래스별 초기화·정리 작업에 쓰여요. 상속 트리에서 각 클래스마다 명시적으로 호출하면서, 보통 특정 순서를 강제할 때 사용하죠. 예를 들어 BUILD 서브메서드로 객체를 만들 때는 가장 파생이 덜 된 클래스부터 가장 많이 된 클래스 순으로 호출돼요. 그래서 가장 많이 파생된(자식) 클래스가, 부모가 이미 초기화됐음을 전제할 수 있는 거예요.
서브메서드는 Submethod 타입이고, submethod 선언자로 선언해요.
class Area {
has $.size;
submethod BUILD(:$x, :$y, :$z) {
$!size = $x * $y * $z;
}
}
서브메서드는 상속되지 않으니까, 오히려 BUILD나 TWEAK 같은 표준 서브메서드에서 호출될 메서드를 만들 때가 재미있는 사용처가 돼요.
class Hero {
has @.inventory;
has Str $.name;
submethod BUILD( :$!name, :@!inventory ) {
@!inventory = self.clean-inventory( @!inventory );
}
submethod clean-inventory( @inventory ) {
@!inventory.unique.sort
}
}
my Hero $þor .= new( name => "Þor",
inventory => ( "Mjölnir", "Megingjörð", "Mjölnir" ) );
say $þor.inventory;
# OUTPUT: «[Megingjörð Mjölnir]»
여기서 BUILD가 clean-inventory를 호출하는데, 이런 메서드들은 자기가 호출된 서브메서드의 맥락 안에서만 의미가 있어요.
Methods
method gist
multi method gist(Submethod:D:)
서브메서드의 이름을 돌려줘요.