Gustav Elmgren
02/11/2022, 11:15 AMclass Test {
val firstValue: String
val secondValue: Long
}
class TestWrapper {
val firstValue: Wrapper<String>
val secondValue: Wrapper<Long>
}
Would it be possible somehow to create a common interface/class for these two classes?
For example, if it would be possible, using union types:
interface InterfaceTest<T : Wrapper<T>, T>
But given that is not possible, is it some other way Kotlin can handle this?ephemient
02/11/2022, 11:20 AMJoffrey
02/11/2022, 11:30 AMGustav Elmgren
02/11/2022, 11:40 AMTest
class based on TestWrapper
via some reflections. And the purpose for the interface would just be for safety. That is that the value the wrapper wraps is the same type as the actual value.Gustav Elmgren
02/11/2022, 11:42 AMclass Test {
val firstValue: String
val secondValue: Long
}
class TestWrapper {
val firstValue: Wrapper<Long>
val secondValue: Wrapper<Long>
}
Here the TestWrapper#firstValue
wrapps a Long
but the Test#firstValue
is of type String
Joffrey
02/11/2022, 11:43 AMTest
class is generated from TestWrapper
via reflection, why would the type not match?Gustav Elmgren
02/11/2022, 11:44 AMJoffrey
02/11/2022, 11:48 AMTest
class from the TestWrapper
class, you're creating Test
instances via reflection from instances of the TestWrapper
class, right?Gustav Elmgren
02/11/2022, 11:49 AMTest
instance (with just one required value), and then I use reflection to set all the values matched (by name and type) via reflection for the other values in the Test
instanceJoffrey
02/11/2022, 11:52 AMGustav Elmgren
02/11/2022, 11:52 AMephemient
02/11/2022, 11:53 AMJoffrey
02/11/2022, 11:53 AMephemient
02/11/2022, 11:53 AMGustav Elmgren
02/11/2022, 11:55 AMephemient
02/11/2022, 11:55 AMJohann Pardanaud
02/11/2022, 1:24 PMinterface ValueProvider<T> {
fun getValue(): T
}
class Value: ValueProvider {}
class ValueWrapper: ValueProvider {}
class Test {
val firstValue: Value<String>
val secondValue: Value<Long>
}
class TestWrapper {
val firstValue: ValueWrapper<String>
val secondValue: ValueWrapper<Long>
}
Then you can create a common interface for your Test
and TestWrapper
classes :
interface InterfaceTest {
val firstValue: ValueProvider<String>
val secondValue: ValueProvider<Long>
}
Joffrey
02/11/2022, 1:27 PMJohann Pardanaud
02/11/2022, 1:42 PMGustav Elmgren
02/11/2022, 1:47 PMJoffrey
02/11/2022, 1:50 PMMichael de Kaste
02/11/2022, 1:57 PM<Type1 | Type2>
and I'm kinda surprised that functionality just plain doesn't exist on declaring level.