android studio will not intelligently switch overloaded methods - android

When you enter the method name, Android Studio will prompt you to choose a different overloading method (with the same function name and different parameters).
For example, I have two functions(It's actually not that simple):
fun sum(a: Int, b: Int){ ... }
fun sum(a: Int){ ... }
When I call, enter the function name to select the first function, then I only enter one parameter, and Android Studio will prompt an error, requiring me to enter two parameters.
If I want to call the second function, I must delete the function name, rewrite the function name, and select the second function when prompted by Android Studio.
What did I miss?

Related

How does high order 'run' function block work in Kotlin?

run signature looks like:
public inline fun <T, R> T.run(block: T.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
The way I see it, the callback (block), is being called via a receiver but without any parameters. So how someone (with a lot of credits here) , said me that:
1.
thing.run(::println)
is equivalent to :
2.
thing.run { println(this) }
I can't understant how the first is running at all... since the way i see it the println won't get any parameter
When you pass a function to a higher-order function by using the function reference syntax instead of a lambda, it doesn't matter if there's a receiver or not. The receiver is like any other parameter, and can be thought of as the first parameter.
So the syntax of run's function parameter:
T.() -> R
will be treated exactly the same as the syntax of let's function parameter:
(T) -> R
so ::println can match either of these.
This works in the other direction, too. When you pass an extension function or member function using function reference syntax, the receiver of the extension function is treated as the first parameter. Either of these is valid:
val list = listOf(1, 2, 3)
list.run(List<Int>::sum)
list.let(List<Int>::sum)
So whether there is a receiver or not only affects lambdas. The actual function signature is the same. You can see this issue if you try to define two functions like this. There will be a compiler error for the two functions having the same signature:
class Foo
fun bar(foo: Foo) {
println("Hello")
}
fun Foo.bar() {
println("Hello")
}

Is vararg in kotlin useless?

I am thinking about vararg (variable arguments) option in kotlin. I know it was here before kotlin in time of java, but I actually never really understand benefit of using it. If i want for example for my function myFun() to take variable number of arguments (let's say Strings), I would create myFun() in a way that it takes List of Strings. I don't see any reason for using vararg.
Is there any benefit of using vararg over List, am I missing something?
vararg gives you freedom to call a function with infinite number of arguments without wrapping them in a collection. Using argument of List type, you are in control of more than just arguments, such as mutability.
This is more opinioned answer.
You are right, passing a list is one option to avoid vararg.
For you as the implementor of the function there is no real difference, but for the consumer it is. It's a question of function design. How do you want your clients to call your functions.
Passing a list forces the caller to create a list first and populate it with all the items before passing them to the function.
fun foo(list: List<Int>) { ... }
val list: List<Int> = listOf(1,2,3,4,5)
foo(list)
If you offer a function taking a vararg type the way how to call that function changes.
fun bar(vararg items: Int) { ... }
fun bar(1) // just passing one parameter
fun bar(1,2,3) // or maybe three, etc.
Both ways have their usecases.

can't understand about () -> Call<WeatherData>

suspend fun safeApiCall(
apiCall: () -> Call<WeatherData>
): Resource<WeatherData> {
apiCall.enqueue() //error Unresolved reference: enqueue
}
In the above code what it the meaning of () -> Call<WeatherData> and how it is different from Call<WeatherData>
Yeah the syntax is explained in the Function types section of #ADM 's link.
Basically apiCall's type is a function with no parameters (the (), like calling someFunction()) and it returns (->) a result with the type Call<WeatherData>. It's the equivalent of this:
fun apiCall(): Call<WeatherData>
except you can pass in any function that matches that signature (same parameter types, same return type). Which means you can pass in lambdas too
safeApiCall({
doStuff()
doMoreStuff()
doThingThatReturnsAWeatherDataCall()
})
(when the lambda is the only parameter it can be moved out of the parentheses, I just wanted to make it clear you're passing it in as a parameter)
If you do have a function declared somewhere that matches the signature, you can pass a reference to that in instead
fun coolWeatherFunction(): Call<WeatherData>
safeApiCall(this::coolWeatherFunction)
(you can drop the this, just showing how you refer to a function on a particular instance or class)
It can be more readable sometimes
"1 2 3 4 5".split(' ').map(String::toDouble).forEach(::println)
1.0
2.0
3.0
4.0
5.0

When is beneficial to pass function type in Kotlin?

private fun createData(
index: Int,
address: () -> Address)
and here is the method call
createData(0){Address(...)}
or I can keep simple and instead I can have
private fun createData(
index: Int,
address -> Address)
and I will call just simple way createData(0,Address(...))
I am just wondering in this case is there any benefit to use function type?
The scenario you're showing is a function take takes a "factory" as a parameter. If you are only ever going to immediately call the function exactly one time, then there is no reason not to just take the Address parameter directly.
But suppose creating an Address is a CPU intensive process and you might not need it until later, if at all. Then you could hold onto the function and only use it later if it is truly needed.
Or suppose you want to create a bunch of Addresses using some kind of input parameter. Then you might want to take a parameter like (Int) -> Address and call it multiple times using different parameters.
Or suppose this function is going to need some number of copies of the Address, and Address doesn't have a copy function. Then you could call the function over and over to get copies.
There are many other uses for higher order functions. I'm just giving a few examples of uses of them for a factory, since that's the kind of example you asked about. But a couple of far more general uses:
If the function is supposed to do an action to multiple objects, and you want callers to be able to pass what that action is.
In place of an interface, you can use a function parameter to allow greater flexibility for what is passed to it (a lambda, a defined function, or a SAM interface)

Kotlin : How the default parameter in " fun main(parameters: Array<String>) " prints "guest" without assigning any values

I am new to kotlin and I have a doubt on the "main" function. When I tried to print the first parameter inside the main function it returns as guest.
From where it returns and I have not assigned any values to the parameters in main function.
Any help should be a great support for my learning
fun main(parameters: Array<String>){
println("kudus, ${parameters[0]}")}
Output Obtained is :
kudus, guest
It is because you pass guest as parameters. If you tried it in online compiler, you will get Exception in thread "main" .java.lang.ArrayIndexOutOfBoundsException: 0
fun main(parameters: Array<String>) {
println("kudus, ${parameters[0]}")
}
It’s not happening by default. Probably your IDE adds a parameter when starting the program. You’ve most certainly configured this at any time before. Check the Run config and remove the argument.

Categories

Resources