use*_*601 5 types signature perl6 raku
Type captures are a cool thing to use for even some simple stuff:
sub assert-same(::T $a, T $b) { ; }
assert-same 1, 2; # silent
assert-same 1, "a"; # type check error
Run Code Online (Sandbox Code Playgroud)
However, the result is non-intuitive for positionals. If I have the following signature
sub foo(Str @bar, Str @xyz) { ; }
Run Code Online (Sandbox Code Playgroud)
Then @bar
is a positional whose elements must be Str
, as is @xyz
. But if I use a type capture, things go weird:
sub assert-same(::T @a, T @b) { ; }
my Str @x = <i j>;
my Str @y = <x y>;
assert-same @x, @y;
# Type check failed in binding to parameter '@b';
# expected Positional[Array[Str]] but got Array[Str] (Array[Str].new("x", "y"))
Run Code Online (Sandbox Code Playgroud)
It seems that the first type capture is capturing via .WHAT
(which makes sense for scalars) rather than .of
which to me is the intuitive sense for positionals, given that the immediate reuse of the capture would only work if .of
had been originally used.
This smells of a bug, but if it's by design, is there a way to enforce via type capture that two typed positionals have elements that are of the same type? In the meantime, I can use
sub assert-same(::T @a, @b where .all ~~ @a.of ) { ; }
my Str @x = <a b c>;
my Str @y = <x y z>;
my Int @i = 1,2,3;
assert-same @x, @y; # silent
assert-same @x, @i; # type check
Run Code Online (Sandbox Code Playgroud)
But that seems a bit silly.
我认为这个区域潜伏着很多错误。我几周前发布了这些:
在发布它们之前,我搜索了 RT 和 GH rakudo 问题队列。在后者中,我提到“cf #2595 和其他类型捕获错误”。也许它已经在那里了;如果没有,请添加问题。TIA。
归档时间: |
|
查看次数: |
60 次 |
最近记录: |