相关疑难解决方法(0)

实例化对象的方法的语法:似乎无法使其正确

我想得到一个指向对象方法的指针,例如这个类

class Foo { 
    has $thing = "baz"; 
    method bar() { say $thing }
}; 

sub quux( Callable $flimflam ) { 
    $flimflam() 
}; 
my $foo = Foo.new;
Run Code Online (Sandbox Code Playgroud)

我想抓住$foo.bar方法指针将它传递给quux.但是,这个

quux(&($foo.bar))
Run Code Online (Sandbox Code Playgroud)

失败了 Type check failed in binding to parameter '$flimflam'; expected Callable but got Bool (Bool::True)? in sub quux

这也不起作用

quux($foo.bar)
Run Code Online (Sandbox Code Playgroud)

也许是因为它没有参数; 但是,如果我们将定义更改bar为:

 method bar($some ) { say $some ~ $thing }
Run Code Online (Sandbox Code Playgroud)

然后上面调用Too few positionals passed; expected 2 arguments but got 1? in …

functional-programming perl6

7
推荐指数
1
解决办法
83
查看次数

指向perl6中的类的构造函数的指针

我正在尝试用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)

如何 …

class perl6 raku

6
推荐指数
2
解决办法
123
查看次数

标签 统计

perl6 ×2

class ×1

functional-programming ×1

raku ×1