Dart静态类型的含义是什么,为什么它与运行时类型不同? [英] What means in Dart static type and why it differs with runtime type?

查看:75
本文介绍了Dart静态类型的含义是什么,为什么它与运行时类型不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dart中存在两种类型.

In Dart exists two kind of types.

  1. 运行时类型
  2. 静态类型

Dart语言规范:

null的静态类型是底部.

  1. null 的运行时类型为 Null
  2. null 的静态类型为 bottom
  1. Runtime type of null is Null
  2. Static type of null is bottom

这意味着Dart中的对象可以具有两种类型.

This mean that objects in Dart can have two kind of types.

一种称为 static 的实数类型和一种 runtime virtual 类型.

One real type that called static and one virtual type that called runtime.

也就是说, null 的运行时类型不是 bottom ,而是常规类 Null .

That is, runtime type of null is not a bottom but a regular class Null.

class Null {
  factory Null._uninstantiable() {
    throw new UnsupportedError('class Null cannot be instantiated');
  }

  /** Returns the string `"null"`. */
  String toString() => "null";
}

但是与此常规运行时类型同时将值 Null 分配给任何其他类型,因为 null 的实(静态)类型是底部类型.

But at the same time value with this regular runtime type Null can be assigned to any other type because real (static) type of the null is a bottom type.

在Dart中如何称呼这项技术?

How called this technique in Dart?

类型替换还是其他?

P.S.

此问题与静态类型的值有关,但与使用类型注释声明的变量的静态类型无关.

This question about static types of values but not about static types of variables that declared with type annotations.

这是因为 null 不是变量,而是具有 bottom static type value .

This is because the null is not a variable but value with static type of bottom.

P.S.

非常奇怪的情况(至少对我而言).

Very curious case (at least for me).

void main() {
  Null _null;
  String s = _null;
}

我收到警告:

A value of type 'Null' cannot be assigned to a variable of type 'String'

这是非常正确的.但这同时可行.

It is quite correct. But at the same time this works.

带有类型替换(静态和运行时)的有趣事物.

Сurious thing with type substitution (static and runtime).

推荐答案

Dart value 的运行时类型是其类.Dart 表达式的静态类型是静态类型推论得出的静态类型,它属于静态类型领域.这个世界不仅限于程序中声明的类.类型"bottom",类型"dynamic"和函数类型"int-> int"都是与类不对应的静态类型的示例.

The runtime type of a Dart value is its class. The static type of a Dart expression is what the static type inference derives it to be, and it belongs to the world of static types. That world is larger than just the classes declared in a program. The type "bottom", the type "dynamic" and the function type "int->int" are all examples of static types that do not correspond to a class.

或者换句话说:值具有类,表达式具有类型(就像许多其他语言一样).没有静态类型的值",因为静态类型在编译时存在,而值仅在运行时[1]存在.

Or in other words: values have classes, expressions have types (just as in many other languages). There is no "static type of values" because static types exist at compile time, and values exist only at runtime [1].

Dart语言规范中指定了静态类型推断算法.它就是它,它所需要的就是在某种程度上与程序的运行时行为兼容.

The static type inference algorithm is specified in the Dart language specification. It is what it is, and all that is required of it is that it is, in some way, compatible with the runtime behavior of the program.

静态类型系统是一种程序分析,试图检测可能的编程错误,仅此而已.如果您有静态类型警告,则可以(但不确定)您有错误.类型推断系统应该只给出很少的错误警告,并且不能检测出很少的实际错误,同时还应该足够简单地描述,理解和实现.

The static type system is a program analysis that attempts to detect likely programming errors, nothing more and nothing less. If you have a static type warning, it is considered likely, but not certain, that you have a bug. The type inference system should give few false warnings, and fail to detect few actual bugs, while also being simple enough to describe, understand and implement.

将底部"选择为空"类型只是使静态类型系统与类型之间的可分配"关系匹配的一种方法,这是分配在运行时检查的,而不必显式检查空"到处.只是一种碰巧会给出有用结果的算法.

Picking "bottom" as the type of "null" is just a way to make the static type system match the "assignable" relation between types, which is what assignments check at runtime, without having to check explicitly for "Null" everywhere. It's just an algorithm that happen to give a useful result.

在运行时静态类型不存在.例如,VM根本不包含任何静态类型系统的实现.

The static type does not exist at runtime. For example, the VM contains no implementation of the static type system at all.

/L

[1]好吧,除了编译时常量表达式.

[1] Well, except for compile-time constant expressions.

这篇关于Dart静态类型的含义是什么,为什么它与运行时类型不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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