gpt4 book ai didi

type-conversion - index() 错误消息对于第三个参数不正确?

转载 作者:行者123 更新时间:2023-12-04 07:03:01 25 4
gpt4 key购买 nike

sub count-a {
my $word = "banana";
my $count = 0;
my $foo; # Source of error: $foo intentionally not given a value.
# The value ought to be zero.

while True {
$foo = index $word, "a", $foo;
last unless $foo.defined;
$foo++;
$count++
}
return $count;
}

say count-a;

这个错误信息是错误的吗?
Cannot resolve caller index(Str: Str, Any); none of these signatures match:
(Str:D $: Cool:D $needle, *%_)
(Str:D $: Str:D $needle, *%_)
(Str:D $: Cool:D $needle, Cool:D $pos, *%_)
(Str:D $: Str:D $needle, Int:D $pos, *%_)
in sub count-a at scrap.p6 line 11
in block <unit> at scrap.p6 line 18

错误消息说 index() 将接受“Any”的第三个参数,这是我用 $foo.WHAT 验证时给出的参数。

最佳答案

third parameter of index is defined as an Int that is actually coerced to Cool ( Coolclass that can represent either a number or a string in Perl 6 )。但是你定义它的方式my $foo (其类型将为“任何”)它不能被识别为其中任何一个。一旦你给它一个值,要么"0"0 , 那可行。

my $foo = 0;  

因为类型将被 index 正确识别的签名。

关于type-conversion - index() 错误消息对于第三个参数不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com