我有以下一些琐碎的脚本:
#!/usr/bin/env perl6
use v6.c;
sub MAIN($x)
{
say "$x squared is { $x*$x }";
}当用实数调用它时,这是非常好的,但是我也想把复数传递给它。当我按原样尝试时,会发生以下情况:
% ./square i
Cannot convert string to number: base-10 number must begin with valid digits or '.' in '⏏i' (indicated by ⏏)
in sub MAIN at ./square line 7
in block <unit> at ./square line 5
Actually thrown at:
in sub MAIN at ./square line 7
in block <unit> at ./square line 5当我改变我的脚本
#!/usr/bin/env perl6
use v6.c;
sub MAIN(Complex $x)
{
say "$x squared is { $x*$x }";
}它完全停止工作:
% ./square i
Usage:
square <x>
% ./square 1
Usage:
square <x>在当前的Perl 6中有任何方法可以做到这一点吗?
发布于 2016-03-08 18:03:24
如果您使用从Str到Complex的Coercive type declaration,它的工作效果要好得多:
sub MAIN(Complex(Str) $x)
{
say "$x squared is { $x*$x }";
}然后:
% ./squared.pl 1
1+0i squared is 1+0i
% ./squared.pl 1+2i
1+2i squared is -3+4i发布于 2016-03-08 18:16:38
https://stackoverflow.com/questions/35872082
复制相似问题