This is the third of a post series where I’m going to talk about some new programming languages with a nice future ahead. During this series I’ll explore deeper the Ceylon, Dart, Elixir, Rust and Swift 2 languages.
The plan is to make a post a week until December 15 (or maybe a little more) and spend 2 weeks exploring each one of them. In the first week I’ll explore the general aspects of the language and make some comparisons with other very known and established languages. In the post of the second week I’ll go deeper inside each one of the languages and explore the individual advantages on use them.
All the posts
- Ceylon Introduction
- Ceylon Usage
- Dart Introduction
- Dart Usage
- Elixir Introduction
- Elixir Usage
- Rust Introduction
- Rust Usage
There are hundred of programming languages out there. Which one should we use? Which help do we have to choose well? How do they compare to each other? This document is an attempt to provide some answers to these questions. Naturally, it would not be possible to provide complete answers: as I mentioned, there are too many programming languages. Nevertheless, we chose five languages with a potential to grow in importance in the coming years. These programming languages are Elixir, Rust, Dart, Swift and Ceylon. During this project, we shall be talking about each one of them. These discussions will be in breath, not in depth. Their goal is to provide the reader with the minimum of information necessary to compare them, and who knows, to lure one or other interested person in learning them in a greater level of details. In any case, we hope to contribute a bit to the popularization of these programming languages, which - likely - will be paramount to the development of computer science in the next ten years.
How did it appear?
Dart is an open source script language focused in web applications and developed by Google1. It was announced at GOTO Conference 2011 (Aarhus, Denmark)2 and its first stable release, Dart 1.0, comes in November 20133.
Why was it designed and implemented?
In the words of Google6:
At Google we’ve written our share of web apps, and we’ve tried in many ways to make improvements to that development process, short of introducing a new language. Now we think it’s time to take that leap. We designed Dart to be easy to write development tools for, well-suited to modern app development, and capable of high-performance implementations.
How can we use it, e.g., install and run?
Dart has it’s own Virtual Machine and has easy installations on all major systems. The current stable version of Dart is 1.12.2. For installation on a Mac you can use Homebrew:
Also you can install the Dartium browser with:
For Linux there are Debian packages and these are the commands for installation on the Debian based systems:
1 2 3 4 5
There are other options to build and install on various Linux distros12.
There is also the versions of the SDK where you can download the portable package and link to your system’s path whatever you want: https://www.dartlang.org/downloads/archive/.
Now let’s check if the installation works. In any path (Dart doesn’t force directory structure) create the file called hello.dart with the following content:
1 2 3
dart hello.dart and the “Hello Dart!” message must show up. Now
Then open the file in a modern browser (like Google Chrome or Mozilla Firefox) and open the browser console (by pressing the F12 key) and the message Hello Dart! is printed in the browser’s console.
I will demonstrate some features of the language by writing some simple programs and analysing some points right after it.
Every Dart program is a library, even if it the program is not written using libraries definitions. Libraries not only provide APIs, but are a unit of privacy: identifiers that start with an underscore (_) are visible only inside the library (like private or protected members in Java).
Libraries can be distributed using packages. Dart has a powerful package manager that resolve package dependencies and package installation as we specify our project dependencies. Dart has also a huge repository of packages[https://pub.dartlang.org/] that delivers the packages listed as project’s dependencies.
Let’s implement a Dart example program that uses the package manager to install a library. This is the code of the program request.dart:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
The program above makes a http get request13 to a document with some phrases (one per line), choose a random line and print the phrase on it. If we try to run this program now, we’ll have some errors:
1 2 3 4 5
As the error is telling us, the program could not resolve the package location.
That happens because the command
import 'package:http/http.dart' as http;
is trying to use a package that is not from Dart’s default SDK and is not
present in our environment. Dart SDK ships with a program called pub. With
this tool we can define our program’s dependencies and install all the possible
package’s dependencies these packages need to work. To configure the project’s
dependencies, we’ll have to use the pub specifications. We define the program’s
dependencies with a file called pubspec.yaml in the root of our project. For
this example, the pubspec.yaml should look like this:
1 2 3
It says that, for our project called request, we have a dependency with the package http, at any version (we are assuming that all http package versions have the functionalities we need to run our program). To install the dependencies, we have to run the command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
As you can see, it downloads lots of other packages, all they are dependencies for the http package (and, or course, for our program). All done, if we try to run the program again, it might work correctly:
1 2 3
Dart is a single-threaded programming language. If any code blocks the thread of execution (for example, by waiting for a time-consuming operation like some heavy I/O operation or a network request) the program effectively freezes. Asynchronous operations let your program run without getting blocked. Dart uses Future objects to represent asynchronous operations.
Lets make a new version of our previous request.dart program. This new version will make 2 HTTP Get request: the usual Ramones quotes and the other one teach us how to say “Merry Christmas” in various languages of the world. This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Running the program, we would have a result like:
1 2 3 4
There is one problem with the program. The program has race conditions.
ramones function is called, it prints a message on the screen and
make the HTTP get request, setting a callback function that choose the line and
prints the message when the response of the request returns. Before the response
natal function is called, printing the message and waiting
for another response from HTTP. Then the two responses are back (on diferent
times) and printed too.
- The function queues up work to be done and returns an uncompleted Future object.
- Later, when a value is available, the Future object completes with that value (or with an error; we’ll discuss that later).
Lets rewrite the program solving the race conditions with futures to see how it works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
We have all those asynchronous methods. We mark'em as asynchronous. We tell the program, which methods it must await the result for continue the execution. That’s all!
-  https://www.dartlang.org/
-  http://gotocon.com/aarhus-2011/presentation/Opening
-  http://news.dartlang.org/2013/11/dart-10-stable-sdk-for-structured-web.html
-  https://code.google.com/p/v8/
-  https://nodejs.org/en/
-  https://www.dartlang.org/support/faq.html#q-why-dart
-  https://www.dartlang.org/tools/dartium/
-  http://news.dartlang.org/2015/03/dart-for-entire-web.html
-  https://www.dartlang.org/support/faq.html#q-will-the-dart-vm-get-into-chrome
-  https://github.com/dart-lang/sdk/wiki/Building
-  https://api.dartlang.org/1.12.2/dart-html/HttpRequest-class.html
-  https://api.dartlang.org/133583/dart-async/Future-class.html
-  https://www.dartlang.org/docs/tutorials/futures/
-  https://www.dartlang.org/articles/futures-and-error-handling/
-  https://pub.dartlang.org/