This is the fourth post of a serie where I’ll 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.
To show some more details on the Dart language, I wrote a webapp example based on an existing Dart sample1. The application is a TODO list app (a list where you insert texts that represent tasks that you have do accomplish and you remove the items on the list that you already did). The application will work on the browser and will be necessary a minimal HTML2 implementation so we can interact with the program. After the code, I will make observations and further explanations. We will need two files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Now the code of the todo.dart file:
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
This file implements all the logic involved in our application. It deals with the addition and removal of the tasks on the list and all the interaction with the HTML5 structure. Those two files composes our example and implements a functional prototype. Now let’s compile and run the application. With the files in the same directory we just have to use the dart2js compiler:
After that, if we open the index.html file in a modern web browser, the application will be working. Insert the tasks inside the text input and click the Add Todo Item button to insert a task in your TODO list. Click then on the [Delete] link in front of the task text to remove it.
Now I will talk about the usage of the Dart language in this small application.
From the very basics, Dart is a class-based, single-inheritance, object-oriented language with C-style syntax.
The static checker acts a lot like in C. It warns you about potential problems at compile-time. Many of these warnings are related to types. The static checker does not produce errors - you can always compile and run your code, no matter what the checker says.
Let’s check out the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The code above sends a warning in
int n = p1 + p2; saying ``A value of
type ‘Point’ cannot be assigned to a variable of type ‘int’``` if we run the
program with the checked mode turned on. However, the program works nice with
no checking at all.
When no types are provided, Dart avoid complaints by adding a default type called dynamic. We can use this type explicitly:
1 2 3 4 5
Dart has generics too. We can have this kind of list:
but you will still be able to write something like
and use as a list of Strings. The previous example is just a shorthand for
The Dart constructor is a method of the class with the same name of the class. As said before, the protected methods starts with underscore and are not visible outside the package of the class.
Just like we saw in the language introduction, our program uses external libraries. The first one is the dart:html library3. This library manages HTML elements and other resources for web-based applications that need to interact with the browser and the DOM (Document Object Model). It includes DOM element types, CSS styling, local storage, media, speech, events, and more.
The second one is the dart:indexed_db library4. The IndexedDB is a web standard for an indexed database which works in the browser. By storing data on the client in an IndexedDB, a web app gets some advantages, such as faster performance and persistence. Many browsers supports the use of IndexedDB5.In our example, we a using this database to store the TODO tasks.
The third and last one is the dart:async library6 we saw in the early examples. In this program, we use async to deal with the events of send and get data from the HTML5 database (IndexedDB).
In our example, none of these libraries is external to the Dart SDK. So we don’t need to install them with the pub get command. They are all included with a Dart default installation.
The Element type in Dart9 is an abstract class, which all HTML elements extend. It implements all HTML browser behaviors like events as we can see in the snippet:
The querySelector method matches a Input button Element field with the a ID attribute equals to “submit” and binds to it an event called onclick. When this button is clicked, the private method _onAddTodo is called.
1 2 3 4
1 2 3
The very next browser feature controlled by Dart in the example is the IndexedDB10 interaction. The Indexed Database API, or IndexedDB (formerly WebSimpleDB), is a recommended web browser standard interface for a local database of records holding simple values and hierarchical objects. IndexedDB was initially proposed by Oracle in 2009.
The code of the program that open the IndexedDB and return the results is:
1 2 3 4 5 6 7
It gets the window of the browser, open the database used by the application, sets the callback function for when the database is opened and ready to use and even sets a callback function to deal with errors, all in that lines.
The method that add things on the database is this one:
1 2 3 4 5 6 7 8 9
It opens a transaction11 with the database, create a object used to store things there and use its method called put passing the JSON object representing the TODO task to it. Same as before, the put method is chained with the methods that declares the callbacks for the function: one for the success and another one for errors.
Very used in our example, a Future is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a Future can register callbacks that handle the value or error once it is available. Those are the callbacks functions I said on the two methods in our previous topic. A interesting thing is that it applies to the methods that deals with Input and Output in the program, always heavy operations in any programming platform.
-  https://www.dartlang.org/samples/indexeddb_todo/
-  https://github.com/dart-lang/dart-samples/tree/master/html5/web/indexeddb/todo
-  https://api.dartlang.org/133612/dart-html/dart-html-library.html
-  https://api.dartlang.org/133612/dart-indexed_db/dart-indexed_db-library.html
-  http://caniuse.com/#feat=indexeddb
-  https://api.dartlang.org/133382/dart-async/dart-async-library.html
-  http://www.w3.org/TR/css3-selectors/
-  https://jquery.com/
-  https://api.dartlang.org/133422/dart-html/Element-class.html
-  http://www.w3.org/TR/IndexedDB/
-  https://en.wikipedia.org/wiki/Database_transaction
-  https://api.dartlang.org/133382/dart-async/Future-class.html
-  https://api.dartlang.org/133382/dart-async/Stream-class.html