这与类的问题类似,但相同的过程似乎不适用于语法。
grammar TestGrammar {
token num { \d+ }
}
my $test-grammar = TestGrammar.new();
my $token = $test-grammar.^lookup('num');
say "3" ~~ $token;
Run Code Online (Sandbox Code Playgroud)
这将返回:
Type check failed in binding to parameter '<anon>'; expected TestGrammar but got Match (Match.new(:orig("3")...)
in regex num at pointer-to-token.raku line 2
in block <unit> at pointer-to-token.raku line 9
Run Code Online (Sandbox Code Playgroud)
这似乎表明您需要绑定到类/语法,而不是“裸”令牌。但是,尚不清楚如何做到这一点。将语法或其实例作为参数传递会返回不同的错误:
Cannot look up attributes in a TestGrammar type object. Did you forget a '.new'?
Run Code Online (Sandbox Code Playgroud)
知道为什么这实际上不起作用吗?
更新:按照上述问题中引用的^find_method方式使用也不起作用。同样的问题。使用也不能解决问题。assuming
更新 2:我似乎有所进展:
my $token = …Run Code Online (Sandbox Code Playgroud) 我正在尝试用Perl 6编写一些类来测试Perl 6类和方法.
这是代码:
class human1 {
method fn1() {
print "#from human1.fn1\n";
}
}
class human2 {
method fn1() {
print "#from human2.fn1\n";
}
}
my $a = human1.new();
my $b = human2.new();
$a.fn1();
$b.fn1();
print "now trying more complex stuff\n";
my $hum1_const = &human1.new;
my $hum2_const = &human2.new;
my $c = $hum2_const();
$c.fn1();
Run Code Online (Sandbox Code Playgroud)
基本上我希望能够使用human1构造函数或human2构造函数来$c动态构建对象.但是我收到以下错误:
Error while compiling /usr/bhaskars/code/perl/./a.pl6
Illegally post-declared types:
human1 used at line 23
human2 used at line 24
Run Code Online (Sandbox Code Playgroud)
如何 …