logo
down
shadow

Kotlin: Generic variable


Kotlin: Generic variable

By : Tum's Zeed ZeedZard
Date : November 21 2020, 04:01 AM
I wish did fix the issue. Hello is it possible to create a generic variable with getter and setter , I think it is not possible. You can use class with generic parameter:
code :
class SomeClass<T> {
    var myVar: T? = null
        set(value) {
            executeCustomFunc(value)
            field = value
        }

    private fun executeCustomFunc(v: T?) {

    }
}
var myVar: Any? = null
    get() { return this }
    set(value) {
        executeCustomFunc(value)
        field = value
    }

private fun executeCustomFunc(v: Any?) {

}


Share : facebook icon twitter icon
Kotlin and generics, implementing abstract generic class with generic array

Kotlin and generics, implementing abstract generic class with generic array


By : Meriem
Date : March 29 2020, 07:55 AM
This might help you You can accomplish this by making your generic parameter a subtype of Vec2t instead of a subtype of Vec2t's generic parameter type (T : Number):
code :
abstract class Mat2t<T : Vec2t<*>>(open var value: List<T>)

class Mat2(override var value: List<Vec2>) : Mat2t<Vec2>(value)
abstract class Vec2t<T : Number> {
    abstract var x: T
    abstract var y: T
}

class Vec2(override var x: Float, override var y: Float) : Vec2t<Float>()

abstract class Mat2t<T : Vec2t<*>> {
    abstract var value: List<T>
}

class Mat2(override var value: List<Vec2>) : Mat2t<Vec2>()
interface Vec2t<T : Number> {
    var x: T
    var y: T
}

data class Vec2(override var x: Float, override var y: Float) : Vec2t<Float>

interface Mat2t<T : Vec2t<*>> {
    var value: List<T>
}

data class Mat2(override var value: List<Vec2>) : Mat2t<Vec2>
Wildcards generic in Kotlin for variable

Wildcards generic in Kotlin for variable


By : Tony Thrall
Date : March 29 2020, 07:55 AM
I wish this help you Is it possible to declare generic wildcards in Kotlin like this code in Java: , The equivalent in Kotlin would be like this:
code :
val a = ArrayList<Int>()
val b: ArrayList<out Number> = a
Java generic to Kotlin generic. Return generic from method

Java generic to Kotlin generic. Return generic from method


By : Stefanos Tsolakidis
Date : March 29 2020, 07:55 AM
help you fix your problem Your code is returning a function which accepts observable of unknown type and returns an Observable object.
The lambda expression you need should be like this:
code :
protected fun <T> onNetworkError(): ObservableTransformer<T, T> {
    return ObservableTransformer { observable ->
        observable
                .doOnError({ throwable -> networkError.setValue(throwable) })
    }
}
kotlin, how to return a generic type member variable

kotlin, how to return a generic type member variable


By : Jim
Date : March 29 2020, 07:55 AM
this one helps. Got error when returning a member variable of generic type (on Android, not sure if it makes any difference) , Here's what you're looking for:
code :
interface IRemoteData<T> {
    fun isSuccess() : Boolean
    fun getJsonPOJO() : T?
    fun getCode() : Int
    fun getMessage() : String
}


class RemoteData<T>(response: Response<ResponseBody>, pojoClassType: Class<T>) : IRemoteData<T> {

    private var mData: T? = null
    override fun getJsonPOJO(): T? {
        return mData
    }

}
interface IRemoteData<String> {
  fun getJsonPOJO(): String?
}

class RemoteData<String>(...): IRemoteData<String> {
  private var mData: String? = null
  override fun getJsonPOJO(): String? {
    return mData
  }
}
Kotlin Upper Bound: What difference does `:Any` make to Kotlin's generic type inference?

Kotlin Upper Bound: What difference does `:Any` make to Kotlin's generic type inference?


By : Mindaugas Dagys
Date : March 29 2020, 07:55 AM
around this issue Following the Kotlin for Android Developers book, we come across extension function ,
Now, as far as I'm concerned, T should imply T:Any
shadow
Privacy Policy - Terms - Contact Us © bighow.org