Skip to content

Instantly share code, notes, and snippets.

@JarnaChao09
Created October 22, 2020 20:11
Show Gist options
  • Save JarnaChao09/f77cc359d08cc1ce3ba64105afe6df51 to your computer and use it in GitHub Desktop.
Save JarnaChao09/f77cc359d08cc1ce3ba64105afe6df51 to your computer and use it in GitHub Desktop.

kotlin fixes many of the issues a lot of people had with java and adds a lot of extra cool features that be utilized to reduce codebase size:

type inference: in java, u have to constantly type out the type of the variable, with type inference, the type of the variable is inferred by the compiler, this cuts down on the verbosity

null safety: null pointer errors are a common thing in java, u constantly have to do if (somevariable != null) checks to make sure things arent null. Kotlins way around this is, to integrate the null safety into the type hierarchy. Every type has 2 technical types, a guaranteed null safe type ie Int, and a nullable type ie Int? this makes null checking really seamless with the type system, getting rid of the nested if null checks with ?. (safe calling) or with !! (double bang) operator

data class: when u need to just hold data in a class, data classes are really nice since they do many of the common methods that u would create normally. a copy method, equals, hashcode, and getters and setters data classes

functional paradigm: functions being first class citizens is something java doesnt do at all, usually when u create a method in java, it has to be tied to a class and we label it static so the method is the same no matter wat instance of the class we create. In kotlin, wat this means is, functions can be declared at top level. A step further is we also get first hand support for lambda types, no more SAM types with functional interface hacks

SAM Type conversion: since lambdas are now a type, kotlin needed a way to easily convert between non-SAM type lambdas and SAM type lambdas, with 1.4 the fun interface was introduced (fun interfaces)[https://kotlinlang.org/docs/reference/fun-interfaces.html]

sealed classes: in java, u arent able to restrict the class hierarchy structure used in an API or codebase. Java 15 added sealed classes preview but kotlin did it first! Sealed classes are used for representing restrictive class hierarches. Think of them as enums in a way, but unlike enums the subclass of a sealed class can have multiple instances and each have unique state sealed classes

Generics: generics support for variance (covariance, contravariance, wildcards, declaration-site variance, type projections, generic functions and generic constraints, star projections) are all possible both in java and kotlin, however, java has a slight limitation type erasure, meaning many things can be implied by high level programmers and devs but the compiler cannot check it directly leading to unchecked casts warnings (that can be surpressed). However, kotlin has a solution to this, inline and reified, inlining a function and setting the generic as reified means it will maintain the knowledge of the type at runtime (low level description is slightly different but thats the general idea). (generics)[https://kotlinlang.org/docs/reference/generics.html] (inlines functions)[https://kotlinlang.org/docs/reference/inline-functions.html#reified-type-parameters]

Operator Overloading: If you are familiar with languages like python, C++, ruby, swift, and C#. They all allow you to define you own operators on certain classes. With java, u would have to write .plus() methods or .minus() methods. Kotlin allows for u to directly manipulate the functionality of + - * / % and many more by labelling a function as operator and naming the function name to the correct operator. Precedence is still kept with overloaded operators operator overloading

Extensions: kotlin gives u the ability to just add on a function to a pre-existing class that u cannot edit (similar to how u can provide extensions in C# or Ruby). This allows u to add on functionalities to classes that u would otherwise have to create a bloated utils class for. On top of this, extensions can be applied to lambdas, called extension lamdbas, this allows for, (wat luke would call hacky), ways of creating lambdas that extend a certain class or give scoped functionalities. This is a major leap forward into a relatively new technology we can create. An internal DSL. Internal DSLs (Domain specific languages) are different from external DSLs. External we create our own parser (this is very powerful, yet hard to maintain). Internal, we use and manipulate wat is possible with the given language and work off of that. With extension lambdas we are now about to do something called scoped functions (context managing as Kotlin Gradle DSL puts it). (extensions)[https://kotlinlang.org/docs/reference/extensions.html] (scope functions)[https://kotlinlang.org/docs/reference/scope-functions.html]

Lastly, Coroutines: coroutines is a huge topic, which i cannot cover in this discord message, however, the run down is the fact that coroutines is a concurrency model that allows for very light weight concurrent programming. Kotlin does this by manipulation of bytecode with continuations. This also allows for many different concurrency models to be use. Generators and async await is supported and can be built on top of coroutines coroutine basics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment