perl6 “P6opaque, Str"vs 简单的“Str"类型 [英] perl6 "P6opaque, Str" vs simple "Str" types
问题描述
我试图从用户输入中获取一个列表,执行我常用的代码,但有时会由于此错误而意外失败:
I was trying to obtain a list from user input doing my usual codes, but sometimes it fails unpredictably due to this error:
This type cannot unbox to a native integer: P6opaque, Str
代码行是
my @a = prompt("Enter list: ").words || (1,2,3);
只有当我只输入一个数字时才会失败.
It failed only if I enter only one number.
何时将 Str 转换为P6opaque, Str"?没有用户意识?我不能使用 +@a[0] 或 @a[0].Int 来转换这个P6opaque, Str"到一个 Int.我在这里错过了什么?
When is a Str converted to "P6opaque, Str" without user awareness? I cannot use +@a[0] or @a[0].Int to convert this "P6opaque, Str" to an Int. What am I missing here?
推荐答案
TL;DR 提到 P6Opaque
主要是一个红鲱鱼.某些代码试图将字符串分配给 int
.您需要先将其强制为 Int
.我知道你已经试过了.剩下的就是找出需要完成的地方.希望这个答案能引导我们到那里.
TL;DR The mention of P6Opaque
is a mostly a red herring. Some code is trying to assign a string to an int
. You'll need to coerce it to an Int
first. I know you've tried that. All that's left is to find out where it needs to be done. Hopefully this answer will guide us there.
将字符串分配给 Int
或 int
是错误的:
It's an error to assign a string to an Int
or an int
:
my Int $a = '1'; # Type check failed ... expected Int but got Str
my int $a = '1'; # This type cannot unbox to a native integer: P6opaque, Str
分配给 Int
的错误被高级机器捕获,它以高级错误消息进行响应.对于 int
,它是低级机制,它以低级消息进行响应.我们将在下面仔细研究这种差异,但就解决您的问题而言,这是一个红鲱鱼.
The error on assigning to an Int
is caught by high level machinery which responds with a high level error message. For int
it's low level machinery which responds with a low level message. We'll take a closer look at this difference below, but it's a red herring as far as fixing your problem is concerned.
要解决此问题,您需要找到字符串被分配或绑定到具有本机整数类型约束(如 int
)的变量的位置,然后在分配之前使用如下内容进行强制:
To fix this problem you'll need to find where a string is being assigned or bound to a variable with a native integer type constraint like int
and then coerce before the assignment with something like this:
my int $a = +'1' # Works
我知道您已经尝试过类似的方法.我不知道为什么它不起作用,因为您尚未共享导致问题的代码部分.
I know you've tried something like that. I don't know why it hasn't worked because you haven't yet shared the part of your code that's causing the problem.
必须直接在您的代码中使用本机整数(即您明确指定本机整数类型,全小写类型,如 int
、int32
、uint
等)或在您的代码使用的某些代码中.
There must be some use of a native integer that's either directly in your code (i.e. you explicitly specified a native integer type, an all lowercase type like int
, int32
, uint
etc.) or in some code your code uses.
因此,请先搜索您的代码.
So, search your code first.
如果您仍然没有找到它,那么请分享足够多的代码以便我们重现问题,最好是在阅读 StackOverflow 的新命名/URL 页面之后 如何创建一个最小的、可重现的示例.TIA.
If you still haven't found it, then please share enough of your code that we can reproduce the problem, preferably after reading StackOverflow's freshly Named/URLed page How to create a Minimal, Reproducible Example. TIA.
P6opaque, Str" vs 简单的Str"类型
"P6opaque, Str" vs simple "Str" types
他们是一样的.P6opaque, Str
是对与 Str
完全相同的类型的引用.
They're the same. P6opaque, Str
is a reference to exactly the same type as Str
.
Str
何时在用户不知情的情况下转换为P6opaque, Str"?
When is a
Str
converted to "P6opaque, Str" without user awareness?
不是.
引用 是repr
和本地表示:
Quoting is repr
and native representations:
P6opaque
是 Perl 6 中所有对象使用的默认表示.
P6opaque
is the default representation used for all objects in Perl 6.
表示是在计算机内存中表示类型的一组规则.
A representation is a set of rules for representing a type in a computer's memory.
与 P6 对象相关的错误通常由 P6 语言/编译器的高级前端"处理.高级错误消息没有提及表示,因为大多数普通 P6 对象都具有相同的表示 (P6Opaque
),即使它们没有,表示仍然不相关.
Errors related to P6 objects are generally handled by the high level "front end" of the P6 language/compiler. High level error messages don't mention representations because most ordinary P6 objects have the same one (P6Opaque
) and even when they don't the representation still won't be relevant.
但这里我们正在处理一个由 MoarVM 处理的错误.
But here we're dealing with an error handled by MoarVM.
如果 MoarVM 的错误消息被认为不相关,则不会提及该表示.例如:
MoarVM's error messages don't mention the representation if it's deemed irrelevant. For example:
my int64 $a = 2⁶³
显示 a Moar 错误关于表示为 P6bigint
的 bigint
类型的消息:
Cannot unbox 64 bit wide bigint into native integer
此错误消息未提及表示形式 (P6bigint
).
This error message doesn't mention the representation (P6bigint
).
但是 MoarVM 对尝试将整数以外的任何内容放入本机整数的响应是 一个 MoarVM 异常,确实提到了这种表示.例如,如果您尝试分配一个 Str
,它是:
But the MoarVM response to trying to put anything other than an integer into a native integer is a MoarVM exception which does mention the representation. For example, if you attempt to assign an Str
it's:
This type cannot unbox to a native integer: P6opaque, Str
如果有人不知道表述,这个消息有点不透明,又名长期协议.但是,在删除表示消除了混淆的同时,它也删除了可能很重要的信息:
If someone doesn't know about representations, this message is bit opaque aka LTA. But while removing the representation removes the confusion it also removes information that might be important:
This type cannot unbox to a native integer: Str
我不相信这实际上更好和/或值得,但如果您对此有强烈的看法,请随时使用 LTA 标签提交有关此问题的 MoarVM 错误.
I'm not convinced that that is actually better and/or worthwhile but if you feel strongly about it, feel free to file a MoarVM bug about this with an LTA tag.
这篇关于perl6 “P6opaque, Str"vs 简单的“Str"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!