有没有一种直接的方法来检查某些东西是否是 mixin? [英] Is there a straightforward way to check if something is a mixin?

查看:71
本文介绍了有没有一种直接的方法来检查某些东西是否是 mixin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Raku mixins 具有两种(或更多)性质,在相同的容器或值以及角色.然而,据我所知,并没有一种直接的方法来检查混合性".在不是由您创建的变量中.

Raku mixins have two (or more) natures, combining several values in the same container, or values along with roles. However, there is not, as far as I can tell, a straightforward way to check for "mixinity" in a variable that has not been created by you.

这可能是个伎俩

my $foo = 3 but Stringy;
say $foo.^name ~~ /\+/;# OUTPUT: «「+」␤»

但是我还缺少其他可以直接查找的属性吗?

But is there any other property I'm missing that would allow to look this up directly?

推荐答案

TL;DR 我不可靠的解决方案[1]更简单比不可靠的解决方案更快[2],并且应该适用于问题 Y 和问题 X[3]:

TL;DR My unreliable solution[1] is simpler and faster than your unreliable solution[2] and should work for question Y and maybe question X[3]:

sub is-mixin ($object) { $object.^is_mixin =:= 1 }
say is-mixin 3;           # False
say is-mixin 3 but 'bar'; # True

脚注

[1] "警告:[role Metamodel::Mixins] 是 Rakudo 实现的一部分,而不是一部分的语言规范."

Footnotes

[1] "Warning: [role Metamodel::Mixins] is part of the Rakudo implementation, and is not a part of the language specification."

[2] 我没有搜索过烤,但可以想象在类型名称中使用 + 只是一种约定,而不是语言规范的一部分.

[2] I haven't searched roast but would imagine that the use of + in type names is just a convention, not a part of the language specification.

[3] 我发现你的问题是XY 问题,根据我在它下面的第一条评论;和/或术语混淆,每秒.这个答案的其余部分解释了我的意思.

[3] I found your question to be an example of either the XY problem, per my first comment below it; and/or of terminological confusion, per my second. The rest of this answer explains what I mean.

有没有一种直接的方法来检查一个容器是否是一个 mixin?

Is there a straightforward way to check if a container is a mixin?

容器"这个词在乐中具有技术意义.它指的是遵循 Raku 的容器协议的值.当值后跟 = 以调用赋值"时,容器协议适用.

The word "container" has a technical meaning in Raku. It refers to a value that follows Raku's container protocol. The container protocol applies when a value is followed by = to invoke "assignment".

您所询问的似乎与这样的 容器 无关,因为它不同于 .并且有充分的理由认为这对您来说无关紧要——您只感兴趣的是测试一个值是否是一个 mixin,而容器"是一个混合变量.方面是一个红鲱鱼,要么是因为您的意思不是容器";在 Raku 中的通常意义上,或者因为容器一个值,所以无论如何它都无关紧要,所以任何用于测试值的方法都可以用于测试容器.

What you are asking about appears to have nothing to do with such a container, as something distinct from a value. And there's good reason to think that that doesn't matter to you -- that what you are just interested in is testing if a value is a mixin, and the "container" aspect is a red-herring, either because you didn't mean "container" in the usual sense in Raku, or because it doesn't matter anyway because a container is a value, so whatever works for testing a value will work for testing a container.

例如,查看您的技巧":

For example, looking at your "trick":

my $foo = 3 but Stringy;
say $foo.^name ~~ /\+/;# OUTPUT: «「+」␤»

这个技巧与使用容器"一词的正常技术用法的任何容器方面都无关.在乐.如果您编写以下不涉及容器的内容,您会得到相同的结果:say (3 but Stringy).^name ~~/\+/;# OUTPUT: «「+」␤».

This trick is unrelated to any container aspect using the normal technical use of the word "container" in Raku. You get the same result if you write the following that doesn't involve a container: say (3 but Stringy).^name ~~ /\+/;# OUTPUT: «「+」␤».

因此,在我的解决方案中,我假设您的意思只是一个,知道您最终的重点是否放在容器上实际上并不重要.

So in my solution I've presumed you just meant a value, knowing that it actually doesn't matter whether or not your ultimate focus is on a container.

如果我的假设是错误的,并且这会产生影响,我敦促您解释您正在尝试做的事情的 X,而不仅仅是 Y.

If I'm wrong about my presumption, and it makes a difference, I urge you to explain the X of what it is you're trying to do rather than just the Y.

这篇关于有没有一种直接的方法来检查某些东西是否是 mixin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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