add_stash — 타입에 스태시 만들고 설정하기
add_stash — 타입에 스태시 만들고 설정하기
Raku에서 패키지 심볼들이 모이는 곳을 스태시(stash)라고 불러요. add_stash는 타입에 그 스태시를 만들어 설정하는 메타클래스 메서드예요. 보통 새 타입을 만드는 마지막 단계에서 호출돼요.
본문
In role Metamodel::Stashing — 타입에 스태시 생성·설정
method add_stash($type_obj)
타입을 위해 스태시를 만들어 설정하고, $type_obj를 그대로 돌려줘요. 이 메서드는 보통 새 타입을 만드는 마지막 단계로 호출되는 편이에요.
어떻게 쓰이는지가 더 와닿도록, 이름 붙이기(naming)와 스태시(stashing)만 지원하는 최소한의 HOW에서 사용하는 예시를 볼게요.
class WithStashHOW
does Metamodel::Naming
does Metamodel::Stashing
{
method new_type(WithStashHOW:_: Str:D :$name! --> Mu) {
my WithStashHOW:D $meta := self.new;
my Mu $type := Metamodel::Primitives.create_type: $meta, 'Uninstantiable';
$meta.set_name: $type, $name;
self.add_stash: $type
}
}
my Mu constant WithStash = WithStashHOW.new_type: :name<WithStash>;
say WithStash.WHO; # OUTPUT: «WithStash»
new_type 안을 보면 빈 타입을 만들고(create_type) 이름을 설정한 다음(set_name), 마지막에 self.add_stash: $type으로 스태시를 붙여요. 그래야 그 타입의 심볼들이 들어갈 자리가 생기는 거죠. WithStash.WHO가 정상적으로 «WithStash»를 출력하는 게 그 증거예요.