首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果dart是静态类型语言,那么"dynamic“关键字是如何工作的?

如果dart是静态类型语言,那么"dynamic“关键字是如何工作的?
EN

Stack Overflow用户
提问于 2022-05-25 07:38:33
回答 2查看 139关注 0票数 0

据我所知,对于静态类型,数据类型在编译时已知,对于动态类型,数据类型在运行时已知/评估。

Dart文档(https://dart.dev/faq#q-is-dart-a-statically-typed-language)说dart是一种静态类型的语言-

代码语言:javascript
复制
Q. Is Dart a statically typed language?
Yes, Dart 2 is statically typed. For more information, read about Dart’s type system.

With its combination of static and runtime checks, Dart has a sound type system, which guarantees that an expression of one type cannot produce a value of another type. No surprises!

Even with type-safe Dart, you can annotate any variable with dynamic if you need the flexibility of a dynamic language. The dynamic type itself is static but can contain any type at runtime. Of course, that removes many of the benefits of a type-safe language for that variable.

那么,我们如何能够分配一个变量“动态”数据类型,这使得类型评估在运行时?

EN

回答 2

Stack Overflow用户

发布于 2022-05-25 08:05:37

简单地说,dynamic ,它本身就是一种类型。它只是将类型检查移到运行时(如您引用的文本中所提到的)。

这允许您执行像final Map<String, dynamic> json = await fetchData();这样的操作,否则这是不可能的。

请注意,Dart强烈不鼓励使用dynamic类型,特别是因为它的运行时影响和缺乏“静态性”。

票数 3
EN

Stack Overflow用户

发布于 2022-05-25 08:07:34

动态是其中一种类型。

因此,如果要使用动态变量,则必须将其转换为其他类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72373634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档