This article talks about the Kotlin programming language. You will learn about the reasons for the appearance of the project, the possibilities of the language and see a few examples. The article is written primarily based on the fact that the reader is familiar with the java programming language, however, knowing a different language, will also be able to get an idea of the subject. The article is superficial and does not address issues related to javascript compilation. On the
official website of the project you can find complete documentation, but I will try to tell you about the language in brief.
about the project
Not so long ago, the company
JetBrains , engaged in the creation of development environments, announced its new product - the programming language Kotlin. A wave of criticism fell upon the company: the critics suggested that the companies should think again and finish the plug-in for Scala, instead of developing their own language. Scala developers really lack a good development environment, but the problems of plug-in developers can be understood: Scala, which was born thanks to researchers from Switzerland, absorbed many innovative scientific concepts and approaches, which made creating a good tool for developing an extremely difficult task . At the moment, the segment of modern languages with static typing for JVM is small, so the decision to create your own language, along with the development environment, looks very far-sighted to it. Even if this language does not take root at all in the community - JetBrains primarily makes it for their own needs. Any Java programmer can understand these needs: Java, as a language, develops very slowly, new features do not appear in the language (we have been waiting for first-order functions for more than a year), compatibility with old versions of the language makes it impossible for many useful things to appear in the near future. the future (for example, decent parameterization of types). For a company that develops software, a programming language is the main working tool; therefore, the effectiveness and simplicity of a language are indicators that affect not only the simplicity of developing tools for it, but also the coding expenses of the programmer, i.e., how simple this code will accompany understand it.
About language
The language is statically typed. But compared to java, the Kotlin compiler adds to the type information about the possibility of the link to contain null, which tightens the type checking and makes execution safer:
fun foo(text:String) { println(text.toLowerCase())
')
Despite the fact that such an approach can save a programmer from a number of problems associated with NPE, for a java programmer, at first it seems superfluous - you have to do unnecessary checks or conversions. But after some time programming on kotlin, returning to java, you feel that you lack this type information, you think about using Nullable / NotNull annotations. The issues of backward compatibility with java are also related to this - there is no such information in java bytecode, but as far as I know, this issue is still in the process of being solved, but for now all the types coming from java are nullable.
By the way, about backward compatibility: Kotlin is compiled into JVM bytecode (the language creators spend a lot of effort on compatibility support), which allows using it in one project with java, and the ability to mutually use the classes java and Kotlin make Kotlin a large threshold java project In this regard, it is important to use multiple java-developments, creating a project entirely on kotlin. For example, I almost had no trouble making a small project based on spring-webmvc.
Let's see a fragment of the controller:
path(array("/notes/")) controller class NotesController { private autowired val notesService : NotesService? = null path(array("all")) fun all() = render("notes/notes") { addObject("notes", notesService!!.all) }
You can see the use of annotations in Kotlin: it does not look as neat as in java (this applies to particular cases, for example, an array of one element), but annotations can be used as self-made keywords like
autowired
or
controller
(if you specify an alias type when importing), and according to the possibilities annotations are close to real classes.
It should be noted that Spring could not wrap kotlin-classes for transaction management - I hope that this will be possible in the future.
The language has support for first-class functions. This means that a function is a type embedded in the language for which there is a special syntax. Functions can be created locally, passed to parameters to other functions, and links to them can be stored:
fun doSomething(thing:()->Unit) {
If we add to this the extension-functions that allow extending an already existing class with a method that does not violate the encapsulation class, but which can be accessed as methods of this class, then we get a fairly powerful extension mechanism that is rather poor in terms of the convenience of standard java libraries. By tradition, we add the ability to filter the list already existing in the standard library:
fun <T> List<T>.filter(condition:(T)->Boolean):List<T> { val result = list<T>() for(item in this) { if(condition(item)) result.add(item) } return result } val someList = list(1, 2, 3, 4).filter { it > 2 }
Pay attention to the fact that the variables do not have types - the Kotlin compiler outputs them if it is possible and does not interfere with the clarity of the interface. In general, the language is made in such a way as to relieve the person behind the keyboard from typing extra characters: a short but clear syntax with a minimum of keywords, no need for semicolons to separate expressions, type inference, where appropriate, the lack of a new keyword to create class - only necessary.
To illustrate the topic of classes and brevity, look at the following code:
// bean- // , // class TimeLord(val name:String) // class TARDIS(val owner:TimeLord) fun main(args:Array<String>) { val doctor = TimeLord("Doctor") val tardis = TARDIS(doctor) println(tardis.owner.name) }
In several lines, we were able to declare two classes, create two objects and display the name of the owner of the TARDIS! You can see that the class is declared with the parameters of its only possible constructor, which are also the declaration of its properties. Extremely short, but informative. Surely there will be those who condemn the impossibility of declaring more than one constructor, but it seems to me that this has its own pragmatism - after all, several designers in java or allow you to declare default parameters that Kotlin supports at the language level, or convert one type to another, with which this class will work, and it is already possible to calmly give it to the factory method. Pay attention to the declaration of "variables" and fields. Kotlin forces us to make a choice:
val
or
var
. Where
val
- declares an unchangeable
final
link, and
var
is a variable, which helps to avoid the ubiquitous use of changeable links.
Example
So we got to a place where you can already do something more interesting. At interviews, I often give the task to implement a tree, make a walk around it, and determine some kind of action with the element. Let's see how this is implemented in kotlin.
So I would like it to look like:
fun main(args: Array<String>) {
Now we will try to implement it. Create a tree node class:
class Node<T>(val value:T) { // private val children:List<Node<T>> = arrayList() fun node(value:T, init:Node<T>.()->Unit = {}):Node<T> { val node = Node<T>(value) node.init() children.add(node) return node } fun traverse(handler:(T)->Unit) { handler(value) children.forEach { child -> child.traverse(handler) } } }
Now add a function to create a tree top:
fun <T> tree(value:T, init:Node<T>.()->Unit): Node<T> { val node = Node(value)
In two places of the code was used construction of the form
Node.()->Unit, , -, Node. , Node.node(), , .
java , Kotlin java . , .
, , github-, Issue Tracker. , . milestone 3, , , JetBrains, .
Node.()->Unit, , -, Node. , Node.node(), , .
java , Kotlin java . , .
, , github-, Issue Tracker. , . milestone 3, , , JetBrains, .
Node.()->Unit, , -, Node. , Node.node(), , .
java , Kotlin java . , .
, , github-, Issue Tracker. , . milestone 3, , , JetBrains, .