dart中的关键字

本文介绍了Dart语言中的关键词,包括上下文关键字、内置标识符和有限的保留单词。Dart允许在某些情况下使用上标标记的关键字作为标识符,但也有特定限制,如不能在异步函数中使用wait或yield作为标识符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道任何一门语言都会有它自己的一套关键词,这个在定义变量的时候是不能使用的,那么在dart中有哪些关键词呢?看官网文档给出的

我们发现有的关键词右上角有数字是什么意思呢?我们把文档看明白就知道了

Avoid using these words as identifiers. However, if necessary, the keywords marked with superscripts can be identifiers:

Words with the superscript 1 are contextual keywords, which have meaning only in specific places. They’re valid identifiers everywhere.

Words with the superscript 2 are built-in identifiers. To simplify the task of porting JavaScript code to Dart, these keywords are valid identifiers in most places, but they can’t be used as class or type names, or as import prefixes.

Words with the superscript 3 are newer, limited reserved words related to the asynchrony support that was added after Dart’s 1.0 release. You can’t use await or yield as an identifier in any function body marked with async, async*, or sync*.

All other words in the table are reserved words, which can’t be identifiers

第一行解释:

避免使用这些单词作为标识符。但是,如果需要,用上标标记的关键字可以是标识符

例子:

void main() {
  var set = const [1,2,3];
  print(set);
}

发现set虽然是关键词但是我们使用set声明称一个变量也是可以的,而if关键词是没有打标记的 是不能当作变量名的

标记1 的作用先看翻译文档:

上标1的单词是上下文关键字,只有在特定的地方才有意义。它们到处都是有效标识符

标记2的作用翻译文档:

带有上标2的单词是内置标识符。为了简化将JavaScript代码移植到Dart的任务,这些关键字在大多数地方都是有效的标识符,但是它们不能用作类或类型名,也不能用作导入前缀

标记3的作用翻译文档:

带有上标3的单词是较新的、有限的保留单词,它们与Dart 1.0发行版之后添加的异步支持相关。不能在任何用async、async*或sync*标记的函数体中使用wait或yield作为标识符

 

这些我们边开发边记住就行!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值