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.
This blog was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-extension-property/ and reposted on Medium on 13th Sept 2020.
A very interesting question asked to me during an interview or even a general situation where we need to add a property (variable) to an existing class without extending it.
According to the theory of extension functions, this is not possible since you can only add the extension functions but not properties, but there is a workaround which can solve the purpose. …
In continuation to my previous post where I explained about Kotlin let scope function, let’s try to understand today about apply function today.
This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-apply-function/ and reposted on Medium on 29th April 2020.
Just to recap, Scope functions are nothing but the functions which define to the scope of the calling object. We can apply operations on that object within that scope and return the object itself from that scope function or we can even return the result of operation or operations from the scope function.
There are a few scope functions
Think about a situation where two or more developers need to modify the same class or add new functionalities to the same class. Or assume they need to implement interfaces to the same class. There will be a merge conflict which needs to be resolved manually or using some tool.
Or think about a situation where an automatically generated code needs to be added into some class without having to recreate the source file.
This post was originally posted at https://agrawalsuneet.github.io/blogs/partial-class-in-csharp-partial-modifier/ and reposted on Medium on 30th Mar 2020.
A similar kind of challenge We faced while working on a Unity project where we two of the devs were constantly working on some same classes. The only way to avoid conflicts was by separating the areas in the class where we both will add the functionalities. Later I came to know about the partial class which was helpful in such situations. …
We all know about three classes
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.
Let try to understand what are these, when to use what and how these are different from one another.
This post was originally posted at https://agrawalsuneet.github.io/blogs/difference-between-any-unit-and-nothing-kotlin/ and reposted on Medium on 3rd Mar 2020.
Any is an open class and by default the superclass for all the classes, whether we define it explicitly or not. …
After reading my last blog about let scope function in Kotlin, a lot of developers have asked me about, why let is an extension to
Template class but not
This post was originally posted at https://agrawalsuneet.github.io/blogs/why-let-function-is-an-extension-to-template-class/ and reposted on Medium on 26th Feb 2020.
let, but other helper functions like
takeUnless are also extension functions to the
Any is the base class for all the class, similar to
java.lang.Object class in Java, even if you extend it or not. …
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.
This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-let-function/ and reposted on Medium on 26th Feb 2020.
Scope functions are nothing but the functions which define to the scope of the calling object. …
“how extension functions resolved?”
This question is being asked by almost everyone both in and outside the interviews.
Even I have asked this question to many candidates during the interview.
The shorted or the only answer I get is “Statically”.
This post was originally posted at https://agrawalsuneet.github.io/blogs/inheritance-vs-extension-functions/ and reposted on Medium on 16th Dec 2019.
What does statically means?
Or how does extension functions are actually resolved?
Let's understand this with an example.
Consider we have two classes BaseClass and DerivedClass.
We added an extension function with the same function name to both the classes.
open class BaseClass
class DerivedClass : BaseClass()
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.
This post was originally posted at https://agrawalsuneet.github.io/blogs/inheritance-vs-extension-functions/ and reposted on Medium on 3rd Dec 2019.
Sometimes we are confused about what to use when. To get help here, one needs to understand first what are these two and what are the benefits and limitations over the other. …
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.
This post was originally posted at https://agrawalsuneet.github.io/blogs/partition-kotlin/ and reposted on Medium on 23rd Oct 2019.
Recently in one of my use case, the basic operations were not enough for me. My use case was very simple. I was having a list on which I need to perform an operation to segregate its items into two lists, one which agrees to some condition and the other which doesn’t. …
While implementing native sharing using my previous medium post, “Native Android screenshot/image sharing in Unity” we saw an exception “android.os.FileUriExposedException” in Android 8 (Oreo) and above.
Since Google changed the sharing way using FileProvider to make it more secure, we can not share the file object directly to any other Application. We need to use the FileProvider and grant read URI permission to make it accessible for any other app.
This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-image-sharing-in-unity-using-fileprovider/ and reposted on Medium on 28th May 2020.
In this Medium blog, will guide step by step to enable screenshot sharing to any other app using FileProvider. After this, you can share your screenshots or image to any other apps on above Android 8 (Oreo) also. …