Perl 6的多调度如何决定使用哪个例程? [英] How does Perl 6's multi dispatch decide which routine to use?

查看:78
本文介绍了Perl 6的多调度如何决定使用哪个例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个程序,在这里我在参数列表中构造一个数组.尽管有一个接受数组的签名,但这称为接受列表的签名:

Consider this program where I construct an Array in the argument list. Although there's a signature that accepts an Array, this calls the one that accepts a List:

foo( [ 1, 2, 3 ] );

multi foo ( Array @array ) { put "Called Array @ version" }
multi foo ( Array $array ) { put "Called Array \$ version" }
multi foo ( List $list )   { put "Called List version" }
multi foo ( Range $range ) { put "Called Range version" }

我从意外的例程中得到了输出:

I get the output from an unexpected routine:

Called Array $ version

如果我取消注释其他签名,则该签名称为:

If I uncomment that other signature, that one is called:

Called List version

为什么不叫( Array @array )版本?调度员如何做出决定(在哪里记录)?

Why doesn't it call the ( Array @array ) version? How is the dispatcher making its decision (and where is it documented)?

推荐答案

我犯了一个非常愚蠢的错误,这就是为什么我没有看到我所期望的.您不能约束以@开头的变量.任何约束都适用于其元素. Array @array表示我有一个位置排序的事物,其中每个元素都是一个Array.这是与raiph说的一样的事情.奇怪的是,语法看起来相同,但功能却有所不同.这是我以前绊倒过的东西.

I made a really dumb mistake, and that's why I wasn't seeing what I expected. You can't constrain a variable that starts with @. Any constraint applies to its elements. The Array @array denotes that I have a positional sort of thing in which each element is an Array. This is the same thing that raiph said. The odd thing is that the grammar looks the same but it does different things. It's something I've tripped over before.

由于它做了一些不同的事情,因此即使数据结构匹配,它也不会解决:

Since it's doing something different, it's not going to work out even if the data structure matches:

foo( [ [1], [2], [3] ] );
foo( [ 1, 2, 3 ] );

multi foo ( Array @array ) { put "Called Array @ version" }
multi foo ( Array $array ) { put "Called Array \$ version" }
multi foo ( List $list )   { put "Called List version" }
multi foo ( Range $range ) { put "Called Range version" }

根据约束和数据结构,我仍然可以获得我不希望的版本:

I still get the version I wouldn't expect based on the constraint and the data structure:

Called Array $ version
Called Array $ version

我认为这将成为普通用户必须学习的Perl 6的缺点之一.

I think this is just going to be one of Perl 6's warts that normal users will have to learn.

这篇关于Perl 6的多调度如何决定使用哪个例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆