How to get a reference to delegated instance in Kotlin using delegation 'by'? - kotlin

Is there any way to get a reference to the delegated object in Kotlin?
Here's an example:
interface A {
fun test()
}
class B: A {
override fun test() {
println("test")
}
}
class C: A by B() {
override fun test() {
// ??? how to get a reference to B's test() method?
}
}

There's currently no way to do that directly. You can achieve that by storing it in a property declared in the primary constructor as follows:
class C private constructor(
private val bDelegate: B
) : A by bDelegate {
constructor() : this(B())
/* Use bDelegate */
}

Another workaround to hotkey's answer might be to include a delegate value
on the A interface...
interface A {
val delegate: A
fun test()
}
class B: A {
override val delegate get() = this
override fun test() {
println("test")
}
}
class C: A by B() {
override fun test() {
delegate.test()
}
}
... this is useful for when zero-arg constructors are required by a framework, e.g. Android Activities

Related

How to create an Enum class that takes multiple parameters but only uses one of them for comparison initialization?

I want my enum class to take two parameters but only use one of them when comparing which type to initialize. Here I want the id to be passed during initialization as well but not use it to control which type gets created.
enum class ActionEnum(val action: String, val id: String) {
URL("URL") {
override fun start() {
openUrl(id)
}
},
START_FRAGMENT("FRAG") {
override fun start() {
startFragmentWithId(id)
}
},
START_POPUP("POPUP"){
override fun start() {
startPopUpWithMessage(id)
}
};
open fun start() {
}
}
From your question is not really clear what you intend to do with the parameter action, but I think that what you are looking for are sealed classes.
Instead of defining an enum, you can define a sealed class like this
sealed class ActionEnum(val action: String, val id: String) {
class URL(action: String): ActionEnum(action, "URL") {
override fun start() {
openUrl(id)
}
}
class START_FRAGMENT(action: String): ActionEnum(action, "FRAG") {
override fun start() {
startFragmentWithId(id)
}
}
class START_POPUP(action: String): ActionEnum(action, "POPUP") {
override fun start() {
startPopUpWithMessage(id)
}
};
open fun start() {
}
}
You can use that like an enum which means that e.g. you have exhaustive when without the need for an else clause:
val a: ActionEnum = ActionEnum.URL("some action")
when(a) {
is ActionEnum.URL -> ...
is ActionEnum.START_FRAGMENT -> ...
is ActionEnum.START_POPUP -> ...
// no more cases possible because ActionEnum is sealed
}
But you can have different instances of each "enum" element where action can have a different value - which is not possible with real enums.

Referring the class that extends this class in a function

https://pl.kotl.in/WJxo0DujU (below is the code in the link)
open class A() { }
class B(): A() {
fun pew2() { }
}
fun <a: A> a.pew() = apply { }
fun main() {
val b = B()
b.pew().pew2()
}
Is there a way to have the function pew() in class A (not in A's companion object) and still be able to type b.pew().pew2() (not b.apply { pew().pew2() })?
You could create the pew() method in A and have the method return the instance itself:
open class A() {
fun pew() = this
}
class B(): A() {
fun pew2() { }
}
fun main() {
val b = (B().pew() as B).pew2()
}
You can override the function in B and narrow the return type.
open class A() {
open fun pew(): A {
//...
return this
}
}
class B: A() {
fun pew2() { }
override fun pew(): B {
super.pew()
return this
}
}
Kotlin doesn't have a self type that would make these kinds of chainable functions easier to implement, but the apply function makes chainable functions that return the same object unnecessary.

Inherit Companion Obejct in Children - Kotlin

I've read that static methods cannot overridden in Kotlin, so I'm not sure if this is possible, but not being able to do so would result in a lot of repetitious code. Is there any way to achieve the same behavior while moving the companion object into the Parent? Here is what I have so far
Parent.kt
abstract class Parent {
protected val TAG = this::class.java.simpleName
}
Brother.kt
class Brother: Parent() {
companion object {
#Volatile private var instance: Brother? = null
fun getInstance() = instance ?: synchronized(this) {
instance ?: Brother().also { instance = it }
}
}
}
Sister.kt
class Sister: Parent() {
companion object {
#Volatile private var instance: Sister? = null
fun getInstance() = instance ?: synchronized(this) {
instance ?: Sister().also { instance = it }
}
}
}
main()
fun main() {
println("Hello, ${Brother.getInstance().TAG}")
println("Hello, ${Sister.getInstance().TAG}")
}
Console Output:
Hello, Brother Hello, Sister
Maybe this will work for what you're trying to do.
You can create a superclass for objects that do this pattern:
open class SingletonAccessor<T: Any> (private val constructor: () -> T){
#Volatile private var instance: T? = null
fun getInstance() = instance ?: synchronized(this) {
instance ?: constructor().also { instance = it }
}
}
And then inherit it from your implementation class companion objects:
class Brother private constructor(): Parent() {
companion object: SingletonAccessor<Brother>(::Brother)
}
class Sister private constructor(): Parent() {
companion object: SingletonAccessor<Sister>(::Sister)
}
This pattern isn't much different from simply making Brother and Sister objects, since they have no constructor parameters, but maybe this is just a simplified example.
Based on #Tenfour04's answer, I've come up with an alternate approach, which incorporates the SingletonAccessor into the Parent
abstract class Parent<T>(private val constructor: () -> T) {
#Volatile private var instance: T? = null
protected val TAG = this::class.java.simpleName
fun getInstance() = instance ?: synchronized(this) {
instance ?: constructor().also { instance = it }
}
}
The implementation in the children is the same as before.
Let me know if this answer can be improved further. In particular, I would like to do in the class declaration class Parent<T: Parent>, but that doesn't compile. Is there a way to limit the type parameter to itself and its children?

How to use multi-generics in modules?

In java, i hava code like below:
public class GenericTest {
private static final GenericTest instance = new GenericTest();
IRequirement requirement;
public static GenericTest getInstance() {
return instance;
}
public class ClassA {}
public interface InterfaceA {}
public void init(IRequirement requirement){
this.requirement = requirement;
}
public interface IRequirement {
<T extends ClassA & InterfaceA> T supply();
}
class ClassB {
void doSomeThing() {
ClassA a = requirement.supply();
InterfaceA ia = requirement.supply();
}
}
}
I can get ClassA or InterfaceA instance according to my needs.
But in kotlin, same codes like below:
open class ClassA(val name: String) {
fun function1() {}
fun function2() {}
}
interface InterfaceA {
fun iFunction1()
}
class ModuleX private constructor() {
var requirement: IRequirement? = null
companion object {
val instance = ModuleX()
}
fun init(requirement: IRequirement) {
instance.requirement = requirement
}
interface IRequirement {
fun <T> supply(): T where T : ClassA, T : InterfaceA
}
}
object ClassB {
inline fun <reified T> doSomeThing() where T : ClassA, T : InterfaceA{
val require = ModuleX.instance.requirement?.supply<T>()
require?.function1()
require?.iFunction1()
}
fun doAnotherThing() {
// IDE give an error when calling supply()
val require = ModuleX.instance.requirement?.supply()
require?.function1()
require?.iFunction1()
}
}
I must specify the actual type of the generic, or use the generic method, otherwise I will not be able to use the IRequirement in the code above, such as in ClassB.doAnotherThing() where IDE give an error "Type inference failed: Not enough information to infer parameter T in fun supply( ): T where T : InterfaceAPlease specify it explicitly."
My question is: In my module, it is required to provide a class that extends ClassA and implements InterfaceAd, but the module does not know the exact type of the class because it is outside the module. How should I use generics in this case?
fun doAnotherThing() {
val require: Any? = ModuleX.instance.requirement?.supply()
if (require != null) {
(require as ClassA).function1()
(require as InterfaceA).iFunction1()
}
}

Why doesn't guice requestInjection work on Kotlin object

I am new to Guice. I am trying to use requestInjection to inject the dependencies of a kotlin singleton object in this way.
APPROACH 1:
class SampleTest {
#Test
fun test() {
Guice.createInjector(object: KotlinModule() {
override fun configure() {
requestInjection(A)
}
})
assertEquals("Hello world", A.saySomething())
}
}
object A {
#Inject
private lateinit var b: B
fun saySomething(): String {
return b.sayHello()
}
}
class B {
fun sayHello(): String {
return "Hello world"
}
}
But I am getting this error:
kotlin.UninitializedPropertyAccessException: lateinit property b has not been initialized
If I change A to a class with no-arg constructor, it works.
APPROACH 2:
class SampleTest {
#Test
fun test() {
val a = A()
Guice.createInjector(object: KotlinModule() {
override fun configure() {
requestInjection(a)
}
})
assertEquals("Hello world", a.saySomething())
}
}
class A {
#Inject
private lateinit var b: B
fun saySomething(): String {
return b.sayHello()
}
}
class B {
fun sayHello(): String {
return "Hello world"
}
}
Instead, if I change requestInjection to requestStaticInjection, it also works.
APPROACH 3:
class SampleTest {
#Test
fun test() {
Guice.createInjector(object: KotlinModule() {
override fun configure() {
requestStaticInjection<A>()
}
})
assertEquals("Hello world", A.saySomething())
}
}
object A {
#Inject
private lateinit var b: B
fun saySomething(): String {
return b.sayHello()
}
}
class B {
fun sayHello(): String {
return "Hello world"
}
}
Why didn't APPROACH 1 work? Why did APPROACH 2 and APPROACH 3 work?
Kotlin's objects are treated as language static singletons, i.e. their initialization/instantiations happens outside the scope of the dependency injection framework.
Therefor, when using the KotlinModule to inject an object, you have to use requestStaticInjection like in APPROACH 3, or change that object to a class, so that the Guice KotlinModule sees it as non-static, as presented in APPROACH 2
Hope that clarifies things a bit.