gpt4 book ai didi

signature - Perl 6 的多分派(dispatch)如何决定使用哪个例程?

转载 作者:行者123 更新时间:2023-12-05 00:15:22 25 4
gpt4 key购买 nike

考虑这个程序,我在参数列表中构造了一个数组。虽然有一个接受 Array 的签名,但这调用了接受 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" }

我从一个意外的例程中得到输出:
Called Array $ version

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

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

最佳答案

Why doesn't it call the ( Array @array ) version?



您的测试 foo 调用只有一个数组( [1,2,3] )作为其参数,而不是 Array s 的数组 (例如 [[1,2,3],[4,5,6]] )。

( @ 中的 @array 表示 does Positional 的值,例如数组或列表。 Array @array 表示相同的内容,但有一个额外的约束,即数组、列表或其他任何元素都是 Array 。)

How is the dispatcher making its decision?



简化,它选择最窄的匹配类型:
multi foo ( Array       )              {} # Narrowest
multi foo ( List ) {} # Broader
multi foo ( Positional ) {} # Broader still
multi foo ( @array ) {} # Same as `Positional`

(Diagram of subtype relationships of Array , List and Positional 。)

有关详细信息,请参阅 jnthn's authoritative answer to a related SO question

(and where is it documented)?



我不确定文档。 Multi-dispatch 看起来非常小。

关于signature - Perl 6 的多分派(dispatch)如何决定使用哪个例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44621439/

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