All Questions
1,547 questions
1
vote
2
answers
24
views
Cannot check for instance of erased type
I have a function which accepts one parameter - Any. There is a when block where I check all possibile types I want to support and throw in case the type is unknown.
fun toAttributeValue(value: Any): ...
4
votes
1
answer
39
views
kotlin behavior with generics seems inconsistent
If I try to compile this code:
fun main(){
val listOfStrings = listOf("hi", "hello")
when(listOfStrings) {
is List<Int> -> println("OK") // ...
-1
votes
0
answers
34
views
Android Custom Image Editor
The function adjusts the crop rectangle position to ensure it fits within the rotated and scaled image bounds.
It respects the target aspect ratio if locked.
Rotation, scaling, and aspect ratio ...
0
votes
2
answers
26
views
How can I use a type parameter to create a generic object?
Let's say I have this class:
class GenericClass<T>(
private val param1: String = ""
private val param2: ((String, String) -> Unit)? = null
) { ... }
and I have these ...
0
votes
1
answer
38
views
Kotlin equivalent of C# new() generic constraint
Is there an equivalent to the following C# code in Kotlin?
public T New<T>() where T:new()
{
return new T();
}
I'm aware I can use reflection to construct types at runtime as well as ...
0
votes
0
answers
22
views
Android Studio / Kotlin: Which code formatter rule controls indentation of this line?
I have the following piece of Kotlin code:
fun test(
param: Pam<
ParamPam,
Param,
PamParam
> // which formatting rule controls indentation of this line?
) {
}
...
1
vote
1
answer
58
views
Proper Kotlin declaration of Generics for higher-order functions
I currently have a situation where I have a couple of data classes (Script and FreeScript) that I want to handle in the same fashion in some code what walks a dataset. For example:
fun toScripts(...
1
vote
2
answers
75
views
Strict generics in Kotlin
Given I have the following code
interface Animal {}
class Cat() : Animal {}
class Dog() : Animal {}
class MyClass<B, A : List<B>>(val list1: A, val list2: A) {}
It looks like I can ...
0
votes
0
answers
38
views
Unit tests to verify override methods with generic parameters
I am trying to add tests to verify the functionality of a derived class overridden method with generics, but I just cannot get it to work. I've tried using a spy as well as partial mocks. I want to ...
1
vote
2
answers
76
views
kotlin get enum value by it's generic type in case it is enum
I'm working on a task to create an API to application string properties which can be represented as Enum, Int, String and probably some more types.
So I tried to write kind of this class:
object ...
0
votes
0
answers
54
views
Kotlin, how to create a generic factory that creates subtypes?
I am struggling to find a way to reduce the factory boilerplate in my code.
I have a base view model class, that all view models extend from. This view model uses generics to determine the type of its ...
0
votes
0
answers
40
views
Kotlin generic interface used in Map of that same interface
I have the following implementations:
interface Validator<T: Request<R>, R> {
fun validate(paymentCmd: T): Pair<ValidationResult, R>
}
The interface Request have nothing, it is ...
1
vote
1
answer
54
views
How to invoke method when parameter is a generic
We have a huge list with implementation instances of a generic interface. Then we get an element of any type. With some filter logic we find the correct instance out of the list.
How can we invoke a ...
1
vote
2
answers
100
views
Meaning of words "in" and "out" in Kotlin generics
I learn kotlin generics but naming makes me crazy.
Let consider 2 code snippets in java and in Kotlin which do the same job:
1. java
public void copy(List<? extends Number> from, List<? ...
0
votes
1
answer
44
views
Type mismatch for custom generic type in Kotlin
In Kotlin I can write:
fun foo(): List<String> {
return ArrayList<String>().apply { this.add("1234") }
}
and it works without any issues.
But I tried to do the same with my ...