What language is Dart ?, Is Dart a language?

Source: Internet
Author: User

What language is Dart ?, Is Dart a language?

I have seen a piece of news over the past few days-is it white compatible? Google is preparing a Java-based Dart application runtime framework for Android. It is unable to comment on news titles and content without commenting. However, Google's introduction of sky seems to prove that native's development approaches are getting closer to the web approach. Recently, react-native is a hot topic, and their principles are similar.Virtual dom treeAnd then only update the modified dom. Sky's readme also said that he had referenced react, and the biggest difference between the two should be the development language. react-native is javascript, while Google uses its own son-Dart.

After the launch of Node. js, The frontend and backend are unified. Now, node development is very popular. Google launched Dart to unify front-end and back-end development, but it has been cool for the past two years. It seems that there are not many developers using Polymer in China, now, I suddenly announced that I would replace Android's development language java with Dart, and held the first Dart Developer Summit. It seems that Google is ready to work on Dart, I believe that Dart will be an important topic at the Google IO conference at the end of May.
Taking advantage of today's holiday, I followed the tutorials on the Dart official website to play Dart, and found that the Dart language is really good.

Reference package

The reference method of the package is similar to that of Python and Go.import:

import 'dart:html';

However, the method for importing only a certain component of the package is special:

import 'dart:math' show Random;import 'dart:convert' show JSON;import 'dart:async' show Future;
Variable

Similar to Python, Dart is a strongly typed language. You do not need to specify the type to define variables. However, Dart also supports specifying the variable type.

static final Random indexGen = new Random();String _firstName;String _appellation;

No DartprivateKeyword. If the variable or method is private, you need to underline the name.

Private variable

class PirateName {  # ...  String _firstName;  String _appellation;  # ...}

Private Method

static _parsePirateNamesFromJSON(String jsonString) {  Map pirateNames = JSON.decode(jsonString);  names = pirateNames['names'];  appellations = pirateNames['appellations'];}
Type conversion

Dart keywordsasFor type conversion.

void updateBadge(Event e) {  String inputName = (e.target as InputElement).value;}
Method

For some specific methods, Dart provides syntactic sugar, which is easy to write. For example, if the expression value is the return value, you can write it as follows:

String toString() => pirateName;

You do not need to write it like this:

String toString() {  return pirateName;}

If yesgetMethod, you can directly add a keyword between the return value type and the method namegetAnd the method name does not need to be enclosed in brackets.

String get jsonString => JSON.encode({"f": _firstName, "a": _appellation});String get pirateName =>    _firstName.isEmpty ? '' : '$_firstName the $_appellation';
Cascade operator (..)

Cascade operators (The cascade operator (..)) Multiple operations can be performed on a member variable.

genButton..disabled = false         ..text = 'Aye! Gimme a name!';

The preceding statements are equivalent

genButton.disabled = false;genButton.text = 'Aye! Gimme a name!';
String Conversion

It is much easier to convert a variable into a string in Dart than Java. It is a combination of Python and you can simply add the $ symbol before the variable name.

'$_firstName the $_appellation';
Constructor

Dart supports name-based constructor, which is more advanced than Java and Python.

PirateName.fromJSON(String jsonString) {  Map storedName = JSON.decode(jsonString);  _firstName = storedName['f'];  _appellation = storedName['a'];}

HerePirateName.fromJSONIs a whole, it should be fully written when used.

return new PirateName.fromJSON(storedName);
Parameters

Like Python, Dart also supportsOptional and Named ArgumentsFor example, the parameter can be written as follows:

PirateName({String firstName, String appellation}) {  # ...}

Parameters must be enclosed in braces and encapsulated into a Python-likeDictionary.

However, no parameters need to be passed during the call.

new PirateName(firstName: inputName)
Generic

Dart supports generics, which is similar to Java. For example, you need to defineListThe variables can be written as follows:

static List names = [];static List appellations = [];

If you want to specify the type, you needListAnd then add the type.

static List<String> names = [];static List<String> appellations = [];

Whether generic types are supported as java doessuper,extendI haven't seen it yet. I don't know it yet.

Asynchronous Operation

The Native Dart language supports asynchronous operations, mainly using two keywordsawaitAndasync.
For example, if we want to define an Asynchronous MethodNew Thread, Directly addasyncThe keyword is OK. In this way,Future, Caller does not need to wait.

static Future readyThePirates() async {  String path = 'piratenames.json';  String jsonString = await HttpRequest.getString(path);  _parsePirateNamesFromJSON(jsonString);    }

awaitSame as the wait method in java, it indicates waiting, but it can only be usedasync.

For example, in the code snippet above,await HttpRequest.getString(path)It means you have to waitHttpRequest.getString(path)ReturnedFutureWith the final result, the execution will continue._parsePirateNamesFromJSON(jsonString);.

I do not know that the Dart branch does not support some high-level language features, such as Closure, Lambda expressions, and generators. I look forward to future studies.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.