Ever imagined calling a public function of a class without dot and parentheses of the parameter in Kotlin. Kotlin provides infix notation with which we can call a function with the class object without using a dot and parentheses across the parameter. Using infix function provides more readability to a function similar to other operators like in, is, as in Kotlin.
…Whatsapp is something which doesn’t require any introduction. It’s a conversation app used by millions of users across the world. These million users make it unique for developers also to give special attention when it comes to sharing some message through Whatsapp.
…Extension functions are really-really helpful while writing code in Kotlin. You don’t have to extend the class but you can add the functionality to even a final (non-open) class which will be resolved statically but how about extension properties.
…In continuation to my previous post where I explained about Kotlin let function, let’s try to understand today about apply
function today.
Collections are a common concept for most programming languages. It is a way of storing similar data types in a single object and iterate over it. Similar to any other language, Kotlin also has Collection defined in kotlin.collections
package.
We all know about three classes Any
, Unit
and Nothing
in Kotlin which have their own functionality and use cases but we usually confuse between the differences among them and when to use what.
After reading my last blog about Kotlin let function, a lot of developers have asked me about, why let
is an extension to Template
class but not to Any
class?
Kotlin has made our life very easy by providing features like extension functions, nullability check and much more. One such kind of really helpful feature is Scope functions. Once you understand what scope functions are, you will not able to resist yourself from using them.
…We all know about inheritance which is there in the OOPs concept where one can extend a class and add functionality to it. In the new programming languages, there is something called as extension functions which can add a functionality to an existing class without extending it.
…The collection is something which is used by almost everyone. It makes our life easy. List
, Set
and Map
are the best examples of them. Even the programming language in which you are writing code provides you with a few basic operations that can be performed on those collections, But sometimes you need even more operations.
Ever thought of chaining an if condition?
What if we can chain the if condition also and moves or executes the next code in the chain only if the condition is true?
…Inlining is basically requesting the compiler to copy the (inlined) code at the calling place.
When a program is executed and a function is called, CPU stores the memory address of the instruction following the function call, copies the arguments of the function into a stack and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register and returns control to the calling function.
…The only thing we learnt about this
pointer in other languages is “this is an implicit pointer which points to the calling object”. As clear from the definition, it points to the object which calls the respective function or property.
Life is painful when you have two or more classes with the same name but different package name and you have to use them both in the same place.
…Have you ever felt some useful functionality missing in an existing class?
The class could be in the libraries provided by language also.
…Sometimes we need a function where we can pass n number of parameters, and the value of n can be decided at runtime. Kotlin provides us to achieve the same by defining a parameter of a function as vararg
. We can pass n number of parameters to a vararg
variable of the defined datatype or even of a generic type.
Sometimes we need to create an object of some class with slight modification, without explicitly declaring a new subclass for it. Java handles this case with anonymous inner classes. Kotlin uses object expression to achieve the same functionality.
…We often need to compare the data of two variables or objects or the references of two objects in Kotlin. This brings in another question, which equality check should we use in which case.
…It is a very common use case where we want to return two values from a method, can be either of same data type or can be of different data types.
…In continuation to my last Medium post Extensions in Kotlin where I explained what are Extensions and how do we use it, this medium post will cover the implementation of Extensions as members of some other class.
…