Autumn  Blick

Autumn Blick

1593322200

Kotlin Simplified - Covariance, Contravariance & inline function

The Covariance & Contravariance
Similar to Java, Kotlin also supports generics which enables us to operate on a method or object without providing actual type at compile time along with type safety.
Kotlin takes it one step further by providing out (covariance) & in (contravariance).
Under the hood it’s as simple as below:
Imagine there are two classes, A is parent/super class & B is child/sub class
open class A {
//some methods & member variable
}
class B : A() {
// some mehods & member variables
}
Covariance:
It means that you can use a subtype in place of a supertype.
Let’s take an example:
class GenericClass<T: A>{
// some actions to be done for GenericClass
}
Now if you try to do something like:
val genericClass:GenericClass = GenericClass()
you will get a compilation error TypeMismatch
Now if we change:
class GenericClass{
// some actions to be done for GenericClass
}
The statement will compile successfully.
Some rules for Covariance When a class or interface generic type is prefixed with out:
It can be only be used in an “out” position, such as a function return type.
It can be also used as val property
It cannot be used as var property or function input
All the collection interface in Kotlin have a generic type defined with out .
Contravariance:
It means that you can use supertype in place of a subtype
Let’s take an example:
class GenericClass<T: A>{
// some actions to be done for GenericClass
}
Now if you try to do something like:
val genericClass:GenericClass = GenericClass
()
you will get a compilation error TypeMismatch
Now if we change
class GenericClass{
// some actions to be done for GenericClass
}
The statement will compile successfully.
Some rules for Contravariance When a class or interface generic type is prefixed with in :
It can only be used in an “in” position such as function input type
It cannot be used as “out” position i.e. cannot be used as val or var property
inline function
Kotlin provides high flexibility & reusability through higher-order functions which takes a lambda as the input parameter. However, this comes with a slight penalty in terms of performance. When you declare a lambda, it’s treated as a normal object in Java. This means, it will have its memory allocated for all the variables used within the lambda and so on.
e.g. We declare a high order function as follows:
fun printSalutation(message: (String) -> String) {
println(message(“MR”))
}
Then you invoke the function:
fun test() {
printSalutation { “Hello $it Foo. How are you?” }
}

#android #covariance #androiddev #contravariance #kotlin-beginners

What is GEEK

Buddha Community

Kotlin Simplified - Covariance, Contravariance & inline function
Autumn  Blick

Autumn Blick

1593322200

Kotlin Simplified - Covariance, Contravariance & inline function

The Covariance & Contravariance
Similar to Java, Kotlin also supports generics which enables us to operate on a method or object without providing actual type at compile time along with type safety.
Kotlin takes it one step further by providing out (covariance) & in (contravariance).
Under the hood it’s as simple as below:
Imagine there are two classes, A is parent/super class & B is child/sub class
open class A {
//some methods & member variable
}
class B : A() {
// some mehods & member variables
}
Covariance:
It means that you can use a subtype in place of a supertype.
Let’s take an example:
class GenericClass<T: A>{
// some actions to be done for GenericClass
}
Now if you try to do something like:
val genericClass:GenericClass = GenericClass()
you will get a compilation error TypeMismatch
Now if we change:
class GenericClass{
// some actions to be done for GenericClass
}
The statement will compile successfully.
Some rules for Covariance When a class or interface generic type is prefixed with out:
It can be only be used in an “out” position, such as a function return type.
It can be also used as val property
It cannot be used as var property or function input
All the collection interface in Kotlin have a generic type defined with out .
Contravariance:
It means that you can use supertype in place of a subtype
Let’s take an example:
class GenericClass<T: A>{
// some actions to be done for GenericClass
}
Now if you try to do something like:
val genericClass:GenericClass = GenericClass
()
you will get a compilation error TypeMismatch
Now if we change
class GenericClass{
// some actions to be done for GenericClass
}
The statement will compile successfully.
Some rules for Contravariance When a class or interface generic type is prefixed with in :
It can only be used in an “in” position such as function input type
It cannot be used as “out” position i.e. cannot be used as val or var property
inline function
Kotlin provides high flexibility & reusability through higher-order functions which takes a lambda as the input parameter. However, this comes with a slight penalty in terms of performance. When you declare a lambda, it’s treated as a normal object in Java. This means, it will have its memory allocated for all the variables used within the lambda and so on.
e.g. We declare a high order function as follows:
fun printSalutation(message: (String) -> String) {
println(message(“MR”))
}
Then you invoke the function:
fun test() {
printSalutation { “Hello $it Foo. How are you?” }
}

#android #covariance #androiddev #contravariance #kotlin-beginners

Juned Ghanchi

1621508255

Kotlin App Development Company in India, Hire Kotlin Developers

We are a prime Kotlin app developer in India. We build and provide the best personalized Android apps, migration services, ongoing maintenance, and management.

We have the most efficient Kotlin developers that build ultramodern, interactive, and secure mobile apps. The technologies we use to create the most advanced Kotlin apps are AR/VR, AI/ML, IoT, etc.

Hire Kotlin app developers in India. Meet us, and we will help you meet all of your technology requirements.

#kotlin app development company india #hire kotlin developers india #kotlin app development company #hire kotlin developers #kotlin development agency #kotlin app programmers

Julie  Donnelly

Julie Donnelly

1602996893

Kotlin: How to Do Higher-Order Functions

Of Course, it has nothing to do with Star Wars 😄

In this article, I want to show you from a beginner perspective how a programmer can implement higher-order functions in the beautiful language Kotlin! I tried to write my code snippets in a way that you can just copy and paste it in your preferred IDE and test it by yourself.

And if you are already an expert you can take a look into my last example 😉

But before we start I quickly want to explain what the special functions are.

In Kotlin you are able to pass a whole function as a parameter into another function or you can return a function from a function. Sounds confusing but in practice it is a lot more easier to understand because every higher order function starts with fun!

#kotlin #kotlin-beginners #higher-order-function #inline #how-to

Vincent Lab

Vincent Lab

1605017502

The Difference Between Regular Functions and Arrow Functions in JavaScript

Other then the syntactical differences. The main difference is the way the this keyword behaves? In an arrow function, the this keyword remains the same throughout the life-cycle of the function and is always bound to the value of this in the closest non-arrow parent function. Arrow functions can never be constructor functions so they can never be invoked with the new keyword. And they can never have duplicate named parameters like a regular function not using strict mode.

Here are a few code examples to show you some of the differences
this.name = "Bob";

const person = {
name: “Jon”,

<span style="color: #008000">// Regular function</span>
func1: <span style="color: #0000ff">function</span> () {
    console.log(<span style="color: #0000ff">this</span>);
},

<span style="color: #008000">// Arrow function</span>
func2: () =&gt; {
    console.log(<span style="color: #0000ff">this</span>);
}

}

person.func1(); // Call the Regular function
// Output: {name:“Jon”, func1:[Function: func1], func2:[Function: func2]}

person.func2(); // Call the Arrow function
// Output: {name:“Bob”}

The new keyword with an arrow function
const person = (name) => console.log("Your name is " + name);
const bob = new person("Bob");
// Uncaught TypeError: person is not a constructor

If you want to see a visual presentation on the differences, then you can see the video below:

#arrow functions #javascript #regular functions #arrow functions vs normal functions #difference between functions and arrow functions

Hire Dedicated Kotlin Developer - WebClues Infotech

The most popular language used for Android app development is Kotlin and recently Kotlin also launched its Cross-Platform app development language Kotlin Multiplatform.

So are you looking to launch a mobile using Kotlin as your Programming Language?

Then you are at the right place as WebClues Infotech offers services to Hire a Dedicated Kotlin Developer who can develop an interactive and rich user-experienced mobile app. Also, WebClues has a flexible pricing model that the business can choose according to its most suitable structure.

So what are you waiting for? Hire a dedicated Kotlin developer from a reputed Web & Mobile app development agency that has successfully served more than 600+ clients.

Get in touch with us!!

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated kotlin developer #hire dedicated kotlin developer #hire kotlin developers #hire kotlin developer #hire dedicated kotlin app developer #hire kotlin app developers india