Dynamic property access in Kotlin - kotlin

From the Kotlin Fundamentals course, we have this code:
#BindingAdapter("sleepImage")
fun ImageView.setSleepImage(item: SleepNight?) {
item?.let {
setImageResource(when (item.sleepQuality) {
0 -> R.drawable.ic_sleep_0
1 -> R.drawable.ic_sleep_1
2 -> R.drawable.ic_sleep_2
3 -> R.drawable.ic_sleep_3
4 -> R.drawable.ic_sleep_4
5 -> R.drawable.ic_sleep_5
else -> R.drawable.ic_sleep_active
})
}
}
In other languages I would simplify this by using the sleepQuality integer to look up the matching element, in Typescript for example:
setImageResource(R.drawable[`ic_sleep_${item.sleepQuality}`] ?? R.drawable.ic_sleep_active)
To start trying this out even my first step doesn't compile:
0 -> R.drawable["ic_sleep_0"] // doesn't compile
Is this kind of operation possible in Kotlin?
Edit/Update
There's a few good responses here.
It looks like for this specific use case, I can look up resources by string, similar to what I'm trying:
val resId = context.resources.getIdentifier("ic_sleep_${item.sleepQuality}", "drawable", context.packageName)
However, this is not a general solution. The following does not work:
val x = item['sleepQuality']
As noted in some responses, this may be possible using reflection. How would this be done?

val resId = context.resources.getIdentifier("ic_sleep_${item.sleepQuality}", "drawable", context.packageName)
setImageResource(if (resId != 0) resId else R.drawable.ic_sleep_active)
Through reflection (based on Getting value of public static final field/property of a class in Java via reflection) :
val resId = try {
R.string::class.java.getField("ic_sleep_${item.sleepQuality}").getInt(null)
} catch (e: Exception) {
R.string.ic_sleep_active
}
setImageResource(resId)

Only using reflection. Kotlin statically typed programming language and does not support "Variable variables"

Related

How to use `when` with 2 sealed classes and getting the inner value?

Consider this extreme simplified code (available on https://pl.kotl.in/bb2Irv8dD):
sealed class Person {
data class A(val i: Int) :
Person()
}
fun main() {
val a = Person.A(i = 0)
val b = Person.A(i = 1)
// Compiles
when (a) {
is Person.A -> print("I have access to {$a.i}")
}
// Does not compile :(
when (a to b) {
is Person.A to is Person.A -> print("I have access to {$a.i} and b {$b.i}")
}
}
Why does the (a to b) code not work? It works for 1 variable, I was hoping I can match on both classes and get both inner values.
The error is:
Incompatible types: Person.A and Pair<Person.A, Person.A> Expecting
'->' Expecting an element Incompatible types: Person.A and
Pair<Person.A, Person.A>
Aside from that syntax not being supported (you can only use is on one thing in a when branch), by using to you're literally creating an instance of the Pair class.
Pair uses generics for the types of its two variables, so this type information is lost at runtime due to type erasure.
So although, you can do this:
when (a to b) {
is Pair<Person.A, Person.A> -> print("I have access to {$a.i} and b {$b.i}")
}
it is only allowed when both a and b are local variables whose types are declared locally, so that the generic types of the Pair are known at compile time. But this makes it mostly useless, because if a and b are local variables with known type at compile time, then you could just replace the above with true or false.
To be able to do something like this in a general way, you must either create local variables to use:
val aIsTypeA = a is Person.A
val bIsTypeA = b is Person.A
when (aIsTypeA to bIsTypeA) {
true to true -> //...
//...
}
or use when without a subject and put the full condition on each branch:
when {
a is Person.A && b is Person.A -> //...
//...
}
The (a to b) returns a Pair<Person.A,Person.A> but what you are checking is Type Person.A to Type Person.A instead of the Type Pair<Person.A,Person.A>.
What you can do instead is:
when (a to b) {
is Pair<Person.A,Person.A> -> print("I have access to {$a.i} and b {$b.i}")
}

Why can't Kotlin infer the parameter and return type of a function?

The Code A is from the end branch of the official sample project.
I think I can simplify it, so I write Code B and Code C, but in fact, they are wrong, why?
BTW, Code D can be compiled.
Code A
val onPeopleChanged: (Int) -> Unit = { viewModel.updatePeople(it) }
Code B
val onPeopleChanged = { viewModel.updatePeople(it) }
Code C
val onPeopleChanged = {it -> viewModel.updatePeople(it) }
Code D
val onPeopleChanged = {it:Int -> viewModel.updatePeople(it) }
From the official documentation it seems that Kotlin cannot infer the types in the lambda expression: refer to example 4 here https://play.kotlinlang.org/byExample/04_functional/02_Lambdas

Using Kotlin streams and filter throws a cannot cast to my custom type exception

I am exploring the equivalent of C# Linq but in Kotlin, so I came across streams:
val c = Manager.customers.entries.stream()
.filter { x -> x.value.name == "Jaime Garcia" }
// after this c is a ReferencePipeline type... apparently. Consider also that Manager.customers is of type HashMap().
And then when I use it:
val id = (c as Customer)?.id
... it throws an error :
java.lang.ClassCastException : java.util.stream.ReferencePipeline$2
cannot be cast to com.example.mypackage.Customer
Maybe I should map c otherwise before using, I tried with something like .collect(Collectors.toMap(a -> a.getValue())) appended after the filter but syntactically it doesn't even work
You should not be using Java's stream API in Kotlin *. Instead you should be using what the Kotlin standard library offers to you to work with collections.
If your customers map looks like this:
val customers = mapOf(1 to Customer("Foo"), 2 to Customer("Bar"))
you can retrieve a list of matching Customers like this:
val c = customers.values.filter { x -> x.name == "Foo" }
Note that c will be a List.
If you want a single element right away, use firstOrNull (for example):
val foo = customers.values.firstOrNull { x -> x.name == "Foo" }
val id = c?.id // id will be null or a Customer
At least as long as you can avoid it. I just found out that the Kotlin standard library offers utility functions to work with Java 8 streams, so it seams that it is not discouraged.
If you want to use proper Kotlin syntax, you can also use find:
Manager.customers.values
.find { it.name == "Jaime Garcia" }
?.id

Use argument passed to when in branch condition in Kotlin?

I have some code that roughly looks like this:
val myObject = myObjectRepository.findById(myObjectId);
when {
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
else -> log.warn("No match, aborting");
}
While this works I would think that the following (which doesn't work) would be an improvement if I only need access to myObject inside the scope of when:
when(myObjectRepository.findById(myObjectId)) { myObject ->
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
else -> log.warn("No match, aborting");
}
The error I get here is:
Unresolved reference: myObject
Can you do something like this in Kotlin and if so how? If not, is there a particular reason for why this shouldn't be allowed?
As shown in the documentation, the proper syntax would be
val myObject = myObjectRepository.findById(myObjectId);
when {
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
else -> log.warn("myObject not found, aborting")
}
Or, to actually match what your first snippet does:
val myObject = myObjectRepository.findById(myObjectId);
when(myObject) {
null -> log.warn("myObject not found, aborting");
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
}
You have to be careful about the syntax. In a while we use an arrow -> which has nothing to do with lambdas. I think this is what you were trying in your example.
The only valid syntax for when is this:
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
On the left side of the arrow -> you declare what the object (x) is being matched against, whereas on the right side you tell what will be executed in that case. Read about this here.
In your example you tried to chain multiple -> which does not work.
This is supported as of Kotlin 1.3. It's referred to as "Capturing when subject in a variable" and looks like this (taken from their documentation):
fun Request.getBody() =
when (val response = executeRequest()) {
is Success -> response.body
is HttpError -> throw HttpException(response.status)
}

Function definition: fun vs val

I'm curious about what is the suggested way to define member functions in Kotlin. Consider these two member functions:
class A {
fun f(x: Int) = 42
val g = fun(x: Int) = 42
}
These appear to accomplish the same thing, but I found subtle differences.
The val based definition, for instance, seems to be more flexible in some scenarios. That is, I could not work out a straight forward way to compose f with other functions, but I could with g. To toy around with these definitions, I used the funKTionale library. I found that this does not compile:
val z = g andThen A::f // f is a member function
But if f were defined as a val pointing to the same function, it would compile just fine. To figure out what was going on I asked IntelliJ to explicitly define the type of ::f and g for me, and it gives me this:
val fref: KFunction1<Int, Int> = ::f
val gref: (Int) -> Int = g
So one is of type KFunction1<Int, Int>, the other is of type (Int) -> Int. It's easy to see that both represent functions of type Int -> Int.
What is the difference between these two types, and in which cases does it matter? I noticed that for top-level functions, I can compose them fine using either definition, but in order to make the aforementioned composition compile, I had to write it like so:
val z = g andThen A::f.partially1(this)
i.e. I had to partially apply it to this first.
Since I don't have to go through this hassle when using vals for functions, is there a reason why I should ever define non-Unit member functions using fun? Is there a difference in performance or semantics that I am missing?
Kotlin is all about Java interoperability and defining a function as a val will produce a completely different result in terms of the interoperability. The following Kotlin class:
class A {
fun f(x: Int) = 42
val g = fun(x: Int) = 42
}
is effectively equivalent to:
public class A {
private final Function1<Integer, Integer> gref = new Function1<Integer, Integer>() {
#Override
public Integer invoke(final Integer integer) {
return 42;
}
};
public int f(final int value) {
return 42;
}
public Function1<Integer, Integer> getG() {
return gref;
}
}
As you can see, the main differences are:
fun f is just a usual method, while val g in fact is a higher-order function that returns another function
val g involves creation of a new class which isn't good if you are targeting Android
val g requires unnecessary boxing and unboxing
val g cannot be easily invoked from java: A().g(42) in Kotlin vs new A().getG().invoke(42) in Java
UPDATE:
Regarding the A::f syntax. The compiler will generate an extra Function2<A, Integer, Integer> class for every A::f occurrence, so the following code results in two extra classes with 7 methods each:
val first = A::f
val second = A::f
Kotlin compiler isn't smart enough at the moment to optimize such kind of things. You can vote for the issue here https://youtrack.jetbrains.com/issue/KT-9831. In case you are interested, here is how each class looks in the bytecode: https://gist.github.com/nsk-mironov/fc13f2075bfa05d8a3c3
Here's some code showing how f and g are different when it comes to usage:
fun main(args: Array<String>) {
val a = A()
exe(a.g) // OK
//exe(a.f) // does not compile
exe { a.f(it) } // OK
}
fun exe(p: (Int) -> Int) {
println(p(0))
}
Where f and g are:
fun f(x: Int) = 42
val g = fun(x: Int) = 42
You can see that g is an object that can be used like a lambda, but f cannot. To use f similarly, you have to wrap it in a lambda.