为什么在这里使用指数枚举键? [英] Why exponential enum keys are used here?

查看:66
本文介绍了为什么在这里使用指数枚举键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处,您可以找到此文件代码:

 枚举NodeFlags {
None = 0,
Let = 1,
Const = 2,
NestedNamespace = 4,
Synthesized = 8,
命名空间= 16,
ExportContext = 32,
ContainsThis = 64,

我在其他地方也看到过这样的数字常量。这样做的目的是什么?



我的猜测是,以后在现有项目之间添加一些新项目已经完成了。尤其是在看到以下内容之后:

  ThisNodeHasError = 32768,
JavaScriptFile = 65536,
ThisNodeOrAnySubNodesHasError = 131072,
HasAggregatedChildData = 262144,
JSDoc = 1048576,
BlockScoped = 3,//三个?

但是在这种情况下,拥有100、200、300等数字不是更容易吗? / p>

另一个猜测是它与按位运算有关,但是在看到 3



FWIW,该列表完全奇怪地结束:

  ReachabilityCheckFlags = 384,
ReachabilityAndEmitFlags = 1408,
ContextFlags = 6387712,
TypeExcludesFlags = 20480,

PS

解决方案

原因是这是因为枚举用作标志枚举。可以使用按位或( | )运算符组合这些值。因此,您可以同时拥有枚举的两个值

  let x = NodeFlags.AwaitContext | NodeFlags.ThisNodeHasError //节点既是await上下文,也有错误

值不能在位级别上互相干扰,因此每个值都必须是2的幂(对于每个幂,在不同的位置上只有一个位设置为1)



基本上,该值的每个位都是一个独立的标志。它们可能在节点上带有单独的标志,例如 isLet isConst isAwaitContext isError 等,但是这在内存方面是浪费的,并且对于编译器而言,因为有很多节点,所以它们是累加的。像这样,一个64位字段可以表示64个标志。



要提取设置的值,可以使用& 运算符,例如 x& NodeFlags.ThisNodeHasError!== 0 表示ThisNodeHasError是在 x 变量中设置的。


Here you can find this code:

enum NodeFlags {
    None = 0,
    Let = 1,
    Const = 2,
    NestedNamespace = 4,
    Synthesized = 8,
    Namespace = 16,
    ExportContext = 32,
    ContainsThis = 64,

And I've seen such numeric constants in other places too. What is the purpose of this?

My guess was that it is done to add some new items later somewhere between the existing ones. Especially after seeing this:

    ThisNodeHasError = 32768,
    JavaScriptFile = 65536,
    ThisNodeOrAnySubNodesHasError = 131072,
    HasAggregatedChildData = 262144,
    JSDoc = 1048576,
    BlockScoped = 3, // three?

But isn't it in this case easier to have numbers like 100, 200, 300 etc.

The other guess was that it has something to do with bitwise operations, but I am not sure at all after I saw 3.

FWIW, the list ends up completely weirdly:

    ReachabilityCheckFlags = 384,
    ReachabilityAndEmitFlags = 1408,
    ContextFlags = 6387712,
    TypeExcludesFlags = 20480,

P.S. Probably exponential is not correct here (so I am sorry and you are welcomed to correct me).

解决方案

The reason is that this enum is used as a flags enum. These values can be combined using the bitwise or (|) operator. So you can have a value that is simultaneously both memebers of the enum

let x = NodeFlags.AwaitContext  | NodeFlags.ThisNodeHasError // The node is both an await context but also has errors

For this to work the values must not interfere with each other at a bit level, so each value must be a power of two (which will only have a single bit set to one at a different position for each power)

Basically each bit in the value is an independent flag. They could have gone with separate flags on the node such as isLet, isConst, isAwaitContext, isError etc, but that would have been wasteful in terms of memory and for a compiler that adds up since there are a lot of nodes. Like this a single field of 64 bits can represent 64 flags.

To extract which value is set you can use the & operator for example x & NodeFlags.ThisNodeHasError !== 0 would mean the ThisNodeHasError is set in the x variable.

这篇关于为什么在这里使用指数枚举键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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