<commit_before># Kotlin Krash Kourse
An introduction to Kotlin from a Nodes Android App Development perspective.
This is by no means meant to be a complete reference to Kotlin. Instead
its a crash course to the various parts of Kotlin that are most relevant to Android developers.


## Mixing Java and Kotlin

## General coding

### Type inference
Kotlin as most modern languages and even C++ (since C++11) implements type inference, which
basically mean that your don't have to write out the type in case it can be inferred.

```kotlin
val i : Integer = 10
```

Can be written as:
```kotlin
val i  = 10 // i is of the type Int
val f = 10f // f is a Float
val d = 10.0 // d is a Double
val str = "Hi Per" // str is a string
```
Because the type of i can be inferred based on the value assignment.
In the next case the type can be inferred because the return type
of myFancyCollection.iterator() is defined somewhere.

```kotlin
val i  = myFancyCollection.iterator()
```

This is purely syntactic sugar but often leads to easier to read code (although it can also
hide information).

### Type Casting

## Data objects

## Listeners and Callbacks
In java listeners and callbacks are generally implemented as interfaces (or abstract base classes).
Often anonymous class instantiations are used to write the code "in place", instead of having
to declare a separate class.

Example of the listener pattern in java:
```java
view.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// do something in response to button clicks
		}
});
```

### Object Expressions

Because most apps - which are event driven - do this a lot Kotlin has a special syntax for this called
object expression. 

Rewriting the above code in kotlin using a object expression:
```kotlin
view.setOnClickListener(object : View.OnClickListener {
	override fun onClick(v: View?) {
		// do something
	}
})
```
object expressions are executed (and initialized) immediately, where they are used.

### Using lambda's for callbacks instead of listeners
Instead of using a listener object expression we can use lambda notation and
declare and pass a anonymous function directly:

```kotlin
view.setOnClickListener { view: View? -> doSomething() }
```
In the above example the function could be defined as:

```kotlin
(View?) -> Unit
```
Which is a function that takes a View? (nullable) and returns void (Unit).
This is a valid type and be used everywhere like a regular old Int:

```kotlin
class MyClass {
    var myCallbackFunction : (success: Boolean) -> Int // this function takes a boolean as an argument and returns an integer
}

// Declare and assign a function to the variable, This implementation calls doSomething() and then return 10
instanceOfMyclass.myCallbackFunction = {success: Boolean ->
	doSomething()
	10
}

// call function
val myInteger = instanceOfMyClass.myCallbackFunction(true)
// myInteger is now 10
```

As usual we can shorten the above with type inference. In the above example we already declared success
as a Boolean, hence that can be inferred with making the assignment:

```kotlin
instanceOfMyclass.myCallbackFunction = {success ->
	doSomething()
	10
}
```

The above lambda's can be written shorter. If you don't need the function parameters, you don't need to specify them.
For instance if you don't care about the view parameter on View.OnClickListener, simply leave it out:

```kotlin
view.setOnClickListener { doSomething() }
```

## Typealias
typealias is a keyword that works a bit like typedef from the C language family. It basically allows you
to substitute/alias a type name for another like this:

```kotlin
typealias MyStringList = List<String>
```

With the above typealias you could write the follow code:

```kotlin
val stringList : MyStringList = ArrayList()
```

Instead of:
```kotlin
val stringList : List<String> = ArrayList()
```

A better example of its usage would be define name for lambda definitions, so if we revisit the earlier example
it could be written like this:

```kotlin
typealias CallbackFunction = (success: Boolean) -> Int

class MyClass {
    var myCallbackFunction : CallbackFunction // this function takes a boolean as an argument and returns an integer
}
```
Now CallbackFunction can be used instead of typing out: (success: Boolean) -> Int, much more readable. 

## Singlejohns and static members
Singletons are basically a way of allowing only one instance of a class. Because this is such an often used
pattern Kotlin supports it directly using Object Declaration:

```kotlin
object MySingleton
{
	fun myFunction() {
		...
	}
}
```
Objects can have supertypes but no constructors (as its always constructed for you).
Object declarations are initialized lazily, when accessed for the first time.


### Using objects from Java
Accessing object members from java uses a special syntax.
Calling myFunction in the object declaration example above looks like this in java:

```java
MySingleton.INSTANCE.myFunction()
```

### Static members 
Kotlin does not have a static keyword like java (same thing can be achieved with the **@JvmStatic** annotation though).
Instead it has a construct called a companion object. Easy way to think of it is that everything inside the companion<commit_msg>wrote something<commit_after># Kotlin Krash Kourse
An introduction to Kotlin from a Nodes Android App Development perspective.
This is by no means meant to be a complete reference to Kotlin. Instead
its a crash course to the various parts of Kotlin that are most relevant to Android developers.


## Mixing Java and Kotlin

## General coding

### Type inference
Kotlin as most modern languages and even C++ (since C++11) implements type inference, which
basically mean that your don't have to write out the type in case it can be inferred.

```kotlin
val i : Integer = 10
```

Can be written as:
```kotlin
val i = 10 // i is of the type Int
val f = 10f // f is a Float
val d = 10.0 // d is a Double
val str = "Hi Per" // str is a string
```
Because the type of i can be inferred based on the value assignment.
In the next case the type can be inferred because the return type
of myFancyCollection.iterator() is defined somewhere.

```kotlin
val i = myFancyCollection.iterator()
```

This is purely syntactic sugar but often leads to easier to read code (although it can also
hide information).

### Type Casting

## Data objects

## Listeners and Callbacks
In java listeners and callbacks are generally implemented as interfaces (or abstract base classes).
Often anonymous class instantiations are used to write the code "in place", instead of having
to declare a separate class.

Example of the listener pattern in java:
```java
view.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		// do something in response to button clicks
	}
});
```

### Object Expressions

Because most apps - which are event driven - do this a lot Kotlin has a special syntax for this called
object expression. 

Rewriting the above code in kotlin using a object expression:
```kotlin
view.setOnClickListener(object : View.OnClickListener {
	override fun onClick(v: View?) {
		// do something
	}
})
```
object expressions are executed (and initialized) immediately, where they are used.

### Using lambda's for callbacks instead of listeners
Instead of using a listener object expression we can use lambda notation and
declare and pass a anonymous function directly:

```kotlin
view.setOnClickListener { view: View? -> doSomething() }
```
In the above example the function could be defined as:

```kotlin
(View?) -> Unit
```
Which is a function that takes a View? (nullable) and returns void (Unit).
This is a valid type and be used everywhere like a regular old Int:

```kotlin
class MyClass {
	// this function takes a boolean as an argument and returns an integer
    var myCallbackFunction : (success: Boolean) -> Int
}

// Declare and assign a function to the variable, This implementation calls doSomething() and then return 10
instanceOfMyclass.myCallbackFunction = {success: Boolean ->
	doSomething()
	10
}

// call function
val myInteger = instanceOfMyClass.myCallbackFunction(true)
// myInteger is now 10
```

As usual we can shorten the above with type inference. In the above example we already declared success
as a Boolean, hence that can be inferred with making the assignment:

```kotlin
instanceOfMyclass.myCallbackFunction = {success ->
	doSomething()
	10
}
```

The above lambda's can be written shorter. If you don't need the function parameters, you don't need to specify them.
For instance if you don't care about the view parameter on View.OnClickListener, simply leave it out:

```kotlin
view.setOnClickListener { doSomething() }
```

## Typealias
typealias is a keyword that works a bit like typedef from the C language family. It basically allows you
to substitute/alias a type name for another like this:

```kotlin
typealias MyStringList = List<String>
```

With the above typealias you could write the follow code:

```kotlin
val stringList : MyStringList = ArrayList()
```

Instead of:
```kotlin
val stringList : List<String> = ArrayList()
```

A better example of its usage would be define name for lambda definitions, so if we revisit the earlier example
it could be written like this:

```kotlin
typealias CallbackFunction = (success: Boolean) -> Int

class MyClass {
	// this function takes a boolean as an argument and returns an integer
    var myCallbackFunction : CallbackFunction 
}
```
Now CallbackFunction can be used instead of typing out: (success: Boolean) -> Int, much more readable. 

## Singlejohns and static members
Singletons are basically a way of allowing only one instance of a class. Because this is such an often used
pattern Kotlin supports it directly using Object Declaration:

```kotlin
object MySingleton
{
	fun myFunction() {
		...
	}
}
```
Objects can have supertypes but no constructors (as its always constructed for you).
Object declarations are initialized lazily, when accessed for the first time.


### Using objects from Java
Accessing object members from java uses a special syntax.
Calling myFunction in the object declaration example above looks like this in java:

```java
MySingleton.INSTANCE.myFunction()
```

### Static members 
Kotlin does not have a static keyword like java (same thing can be achieved with the **@JvmStatic** annotation though).
Instead it has a construct called a companion object. Easy way to think of it is that everything inside the companion
{'fraction_non_alphanumeric': 0.07051221134657418, 'fraction_numerical': 0.003611137508315119, 'mean_word_length': 4.38863287250384, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4453876', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2652, 'n_words': 1484}