简体   繁体   English

为什么 Dart 的类型注解在左边?

[英]Why are Dart's type annotations on the left?

New languages, TypeScript, Kotlin, Scala, and Rust all have type annotations on the right, but Dart has type annotations on the left.新语言 TypeScript、Kotlin、Scala 和 Rust 都在右侧有类型注释,但 Dart 在左侧有类型注释。 Is this designed that way for a reason or benefit?这是出于某种原因或利益而设计的吗? Is there a possibility that the type annotation will be changed to the right in the future roadmap or when Dart3 is developed?在未来的路线图中或开发 Dart3 时,类型注释是否有可能更改为右侧? I like most of Dart, but I don't like this syntax.我喜欢 Dart 的大部分内容,但我不喜欢这种语法。 I have looked for a thread discussing this but could not find one.我一直在寻找一个讨论这个问题的线程,但找不到一个。 Is it discussed anywhere?有没有在任何地方讨论过?

The Dart syntax was inspired by Java/C#/JavaScript, which are all from the C-family of language syntax. Dart 语法的灵感来自 Java/C#/JavaScript,它们都来自 C 系列语言语法。 Dart is in that family too.达特也在那个家庭。

Using a C-like syntax means putting types before identifiers in declarations, and using the placement to recognize what is a type and what is not.使用类似 C 的语法意味着在声明中将类型放在标识符之前,并使用该位置来识别什么是类型,什么不是。 That's why you can use the same names for types and variables.这就是您可以为类型和变量使用相同名称的原因。 (Not that you should, but the grammar doesn't care). (不是你应该,但语法并不关心)。

The goal was familiarity to users of those languages.目标是让这些语言的用户熟悉。 It was an explicit goal to be unsurprising to users of those languages.让那些语言的用户不感到惊讶是一个明确的目标。

Dart also has other C-syntax idiosyncrasies, like the conditional operator ? Dart 还有其他 C 语法特性,例如条件运算符? / : , or the for(..;..;..).. loop, and ending statements with a ; / :for(..;..;..)..循环,并以;结束语句. . It's familiar, if that's the thing you are already familiar with.它很熟悉,如果那是你已经熟悉的东西。

Having a C-like syntax has its down-sides too, when it comes to adding new language features.在添加新的语言特性时,使用类似 C 的语法也有其缺点。 Because the C syntax is so grammatically dense, there's not much room to add new syntax in the gaps.因为 C 语法在语法上非常密集,所以在空白处添加新语法的空间不大。 Most things already mean something, and are there for a reason (it's incredibly hard to, eg, remove semicolons, because the grammar isn't built to be able to guess where a new statement start.) On the other hand, it's syntactically dense too, aka "not verbose".大多数事情已经有意义,并且存在是有原因的(例如,删除分号非常困难,因为语法不是为了能够猜测新语句的开始位置而构建的。)另一方面,它在语法上很密集也,又名“不冗长”。 That's usually considered a good thing.这通常被认为是一件好事。

As for odds of a large syntax change, like moving types to the right, in Dart 3.0 ... I'd say 0.0%.至于在 Dart 3.0 中发生较大语法变化的几率,比如将类型向右移动……我会说 0.0%。

It has to be an incredibly valuable change to outweigh having to migrate every Dart file in existence .它必须是一个非常有价值的更改,而不是必须迁移现有的每个 Dart 文件 Simply moving types to a another place in the code isn't going to be that by itself.简单地将类型移动到代码中的另一个位置本身并不会如此。 It would have to be a complete re-imagining of the entire language syntax.它必须是对整个语言语法的完全重新想象。 And should still keep supporting the old syntax forever.并且应该仍然永远支持旧语法。 (Maybe Dart 10.0 NSE - New Syntax Experience!) (也许 Dart 10.0 NSE - 新的语法体验!)

Why are Dart's type annotations on the left?为什么 Dart 的类型注解在左边?
Dart (Dash) was intended to replace JavaScript. Dart (Dash) 旨在取代 JavaScript。
The developers of Dart did not hesitate (unlike the developers of other modern languages) to copy most of the syntax from JavaSctipt and Java. Dart 的开发人员(与其他现代语言的开发人员不同)毫不犹豫地复制了 JavaSctipt 和 Java 的大部分语法。
The main reason is to copy the syntax of popular programming languages.主要原因是复制流行编程语言的语法。
Not the best, but the easiest way.不是最好的,但最简单的方法。
Suffice it to recall the long-standing problem with the definition of the Function type.回忆一下Function类型定义的长期存在的问题就足够了。

All this suggests that the developers of Dart (Dash) did not initially plan to invest in developing a new and modern syntax for the new language (unlike other developers who did not go down the simple path of copying the syntaxes of existing languages).所有这些都表明 Dart (Dash) 的开发人员最初并没有计划投资为新语言开发新的现代语法(不像其他开发人员没有走复制现有语言语法的简单路径)。
It can be assumed that they chose this simplest way to develop the latest programming language, since the developers were not sure that the main goal would be achieved.可以假设他们选择了这种最简单的方法来开发最新的编程语言,因为开发人员不确定主要目标是否会实现。 And, as we all know, the main goal was to create a language that would become a replacement for JavaScript.而且,众所周知,主要目标是创建一种可以替代 JavaScript 的语言。
But the miracle did not happen and Dart (Dash) never became a replacement for JavaScript.但奇迹并没有发生,Dart (Dash) 也从未成为 JavaScript 的替代品。
In this case, this may partly justify why the funds were initially saved on the development of language design for something that other developers of programming languages ​​did not save on.在这种情况下,这可能部分地证明了为什么最初将资金用于语言设计的开发是为了其他编程语言开发人员没有节省的东西。 In this case, this is giving the language a modern look and, most importantly, a practical design.在这种情况下,这为语言赋予了现代外观,最重要的是,赋予了实用的设计。

PS附言
Apparently, if something is very good to imitate and mix two (popular) into one and slightly modify it (but obligatory not to overdo it in innovations), then you can count on success only on the basis of this.显然,如果某些东西非常好模仿并将两个(流行的)合二为一并稍微修改它(但在创新中必须不要过度),那么您只能在此基础上指望成功。

Please excuse me for the inaccuracy.请原谅我的不准确之处。 The main goal was not just to replace JavaScript, but ultimately to replace JavaScript.主要目标不仅仅是取代 JavaScript,而是ultimately取代 JavaScript。

The goal of the Dash effort is ultimately to replace JavaScript as the lingua franca of web development on the open web platform.

Link to full text: https://gist.github.com/paulmillr/1208618全文链接: https ://gist.github.com/paulmillr/1208618

...Browser VM -- Our aspiration is that Dash will ultimately be a viable substitute for Javascript as the native client-side language of choice across all browsers. ...浏览器虚拟机——我们的愿望是,Dash 最终将成为 Javascript 的可行替代品,作为所有浏览器选择的本地客户端语言。

Oh...哦...
At the same time, at the initial stage, nothing was done to make the design of the language practical (that is, suitable for future improvement of its construction).同时,在初始阶段,没有做任何事情来使语言的设计实用(即适合其结构的未来改进)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM