在 Raku 中在调用者的类型身上使用 :D 或 :U 类型笑脸来制造 type/instance 方法:
class Foo {
multi method foo (Foo:D:) { say "instance" }
multi method foo (Foo:U:) { say "type object" }
}
Foo .foo; # 输出 type object
Foo.new.foo; # 输出 instance
# Can use compile time vars to aovid re-typing the actual name everywhere:
class Bar {
multi method foo (::?CLASS:D:) { say "instance" }
multi method foo (::?CLASS:U:) { say "type object" }
}
Bar .foo; # "type object"
Bar.new.foo; # "instance"




近期评论