Why does Kotlin use a form of declaring functions and variables different from "traditional"?

8

Traditionally the return type and variable type are indicated at the beginning, before the name:

public int soma(int a, int b){
    return a + b;
}

In Kotlin it is indicated after:

fun sum(a: Int, b: Int): Int {
    return a + b
}

What are the fundamentals behind this option?

    
asked by anonymous 08.08.2017 / 17:27

2 answers

7

Exactly, this "traditional" well quoted. The form that calls traditional comes from the C style, which is considered bad by many.

Complicates the compiler because it generates context for the parser , makes it difficult to type in, requires a keyword before the statement when it does not mean the type (although Kotlin missed this, in my opinion ).

Many people also find it more readable like this, it gets more fluid. The variable name is more important than its type. In fact in types like big names until you find the name of the variable is a sadness:

val shapeInfo: HashMap[Shape, (String, String)] = makeInfo()
val HashMap[Shape, (String, String)] shapeInfo := makeInfo()

What do you find the variable name more easily?

* I would have preferred something like this:

variavel := 1
constante ::= 1
variavel : Int

It's even easier to find the name, is not it? Always in column 0.

Identifiers are very important in the code, giving visibility to them is critical.

    
08.08.2017 / 17:51
3

A FAQ had an answer regarding that , but was removed:

  

Why have type declarations on the right?

     

We believe it makes the code more readable. Besides, it enables some   nice syntactic features, for instance, it's easy to leave type   annotations out. Scala has also been very well   problem.

Or, in free translation:

  

Why type declarations on the right?

     

We believe it makes the code more readable. In addition, it allows the addition of   some interesting features of syntax. It is easy, for example, to leave out type notes.   Scala also proved that this is not a problem.

    
08.08.2017 / 18:12