
A few days ago I decided to feel the state at which time the Dart language was represented some time ago. The easiest way, in my opinion, to try the possibilities of a language is to write something simple and complete on it. Without thinking twice, I decided to write a simple logger for Dart in object-oriented style. Under the cut out my impression of Dart and a brief description of what happened.
Dart, as you know, is under active development and currently has:
- Virtual machine to execute code locally
- Dart-code translator to Javascript
- A simple development environment Dart Editor, built on the basis of Eclipse and in the alpha version stage.
- A specialized Chromium assembly called Dartium, which allows you to run Dart applications, without being translated into Javascript.
Impressions of the language itself
The easiest thing in my opinion is to write in Dart to anyone who has ever written in Java, because the Dart syntax borrowed a lot from this language. However, this gives rise to the feeling that someone was very abused over Java:
- Strange file naming conventions. In the description of the recommended code design rules, it is written that * .dart files should be named with a small letter and with underscores. As I understand it, the main reason for this decision is that not all operating systems are case sensitive. Nevertheless, it is very strange to open a file with the name in lower case and find a class there.
- The location of the code in the files. In Dart there is complete chaos in the location of the code in the files. The code can be contained in both classes and outside classes. In this case, the entry point in the program is the void main () method. Even the code of the standard Dart library is a strange mixture of procedural and object-oriented code. A single file may contain a class definition and functions located outside the classes.
- The absence of some OOP modifiers. Dart is missing the private keyword, which in most C-like languages ​​is a private class method. Instead, the names of private methods and fields must begin with an underscore, which is only an agreement in some other programming languages.
- Syntactic sugar. The merit of the language is put the presence of some structures that can be considered nothing more than syntactic sugar. For example, if a method has a short implementation, then it can be written in the same line as the signature, separating them with the arrow => . There are also the keywords get \ set, denoting methods — getters and methods — setters.
- Strange recommendations on the design of the code. Dart is basically a strongly typed language, which opens up tremendous opportunities for quick refactoring and catching errors while writing a program. However, it is recommended not to specify types for local variables in the code style guide. Well, very, very strange ...
Impressions from the development environment
It's all very simple - the environment is damp and with a lot of bugs. First of all, the bad work of autocompletion and code highlighting are annoying. For example, autocompletion refuses to show closed class methods within the class itself. Although the Java support provided by Eclipse DartEditor is still far away, the environment nevertheless catches the most obvious errors related to the incorrect import of libraries or syntax errors in the code. It even shows some features of the standard Dart library - for example, that you cannot simultaneously import client (dart: html) and server libraries (dart: io).
Promised Sample Code
Notwithstanding the fact that similar functionality is available in the standard library, I wrote a
very simple logger that allows you to output messages with different levels of logging (Debug, Info, Alert, etc.) to various output streams. The main abstractions used in the logger are:
- core / Severity.dart - sets the message severity level from DEBUG to EMERG.
- core / LogEntry.dart - is a single log entry that stores the text message and the importance of the message.
- streams / Stream.dart - message output stream, sets the way messages are displayed to the user. Each of the above output streams is inherited from this class. You can also create your own output stream.
At the moment, 3 output streams are implemented:
- streams / StdoutStream.dart - standard output stream using print ()
- streams / WebConsoleStream.dart - output to the web developer console in the browser
- streams / FileStream.dart - output to file
Using the logger is very simple:
#import("vader_server.dart"); void main(){ Vader vader = Vader.vader(new StdoutStream()); vader.logWarn("You don't know the power of the dark side!"); vader.logInfo("Luke, I am your father!"); }
To get started, just import one of the library files:
vader_server.dart (for logging to a file) or
vader_client.dart (for logging in a browser). This separation is due to the fact that it is impossible to import client and server libraries into Dart at the same time. The logger code, provided with comments, can be viewed at
this link .