Size: a a a

2019 October 08

MG

Matthew Good in Kotlin Start
Andrew Mikhaylov
As in general case Generic<T> implicitly means Generic<T: Any?>, which is not a subtype of Generic<Any>.
so why do i get Type mismatch: inferred type is Vector<Byte> but Vector<Any?> was expected
источник

MG

Matthew Good in Kotlin Start
Any? is MEANT to accept any possible type right?
источник

D

Denys in Kotlin Start
Matthew Good
Any? is MEANT to accept any possible type right?
What's inside the Vector<>?
источник

MG

Matthew Good in Kotlin Start
Denys
What's inside the Vector<>?
what do you mean
источник

А

Андрей in Kotlin Start
What?
источник

D

Denys in Kotlin Start
Matthew Good
what do you mean
Sorry, I misread the question.

Show us this part of the code.
источник

AL

Alexander Levin in Kotlin Start
Matthew Good
so why do i get Type mismatch: inferred type is Vector<Byte> but Vector<Any?> was expected
Please read all messages. I mentioned the problem with invariance of generic type before twice.

If you want to provide Vector<Byte> as Vector<Any?>, your generic type should be covariant. For example, in VectorBase/Core example it can be done something like this:

open class VectorCore<out Type> {}

open class VectorBase<out Type>: VectorCore<Type>() {
   fun elementWiseOperation(vector: VectorCore<Any?>, action: (result: VectorCore<Any?>, lhs: Any?, rhs: Any?) -> Unit): VectorCore<Any?> = TODO()

   fun t() {
       val A = elementWiseOperation(this) { _, _, _ -> }
   }
}

Important thing is <out Type> in the class declaration

You can read more about that problem here: https://kotlinlang.org/docs/reference/generics.html#variance
источник

MG

Matthew Good in Kotlin Start
why? using Any? SHOULD solve this right?

    open class Vector<Type> : VectorBase<Type> {
       operator fun plus(vector: Vector<Any?>): Vector<Any> = elementWiseOperation(vector as Vector<Any>) { result, lhs, rhs ->
           result.addDimension(addition(lhs, rhs))
       } as Vector<Any>
   }
   val v1 = Vector<Byte>(3)
   v1[0] = 1
   v1[1] = 2
   v1[2] = 3
   val v2 = Vector<Byte>(3)
   v2[0] = 1
   v2[1] = 2
   v2[2] = 3
   println(v1)
   println(v2)
   println("v1 + v2 = ${v1 + v2}")
источник

AL

Alexander Levin in Kotlin Start
Matthew Good
why? using Any? SHOULD solve this right?

    open class Vector<Type> : VectorBase<Type> {
       operator fun plus(vector: Vector<Any?>): Vector<Any> = elementWiseOperation(vector as Vector<Any>) { result, lhs, rhs ->
           result.addDimension(addition(lhs, rhs))
       } as Vector<Any>
   }
   val v1 = Vector<Byte>(3)
   v1[0] = 1
   v1[1] = 2
   v1[2] = 3
   val v2 = Vector<Byte>(3)
   v2[0] = 1
   v2[1] = 2
   v2[2] = 3
   println(v1)
   println(v2)
   println("v1 + v2 = ${v1 + v2}")
Wrong.  Nullability was also the problem, but not the only one. Please read the previous message.

Summary:
You can't put Vector<Type> as Vector<Any> because of the variance problem and nullability problem.
First problem can be solved with covariance.
Second problem can be solved either with non-nullable generic type (<out Type: Any>) or allowing nullable types in the code.

P.S. Please do not use casting ( as Vector<Any> ) in Kotlin just because you have problems with types, it is pretty unsafe :)
источник

AM

Andrew Mikhaylov in Kotlin Start
Yeah, my bad, forgot about variance.
Should have checked that myself via play.kotl.in :)
источник

А

Андрей in Kotlin Start
ok
источник
2019 October 09

MG

Matthew Good in Kotlin Start
Alexander Levin
Wrong.  Nullability was also the problem, but not the only one. Please read the previous message.

Summary:
You can't put Vector<Type> as Vector<Any> because of the variance problem and nullability problem.
First problem can be solved with covariance.
Second problem can be solved either with non-nullable generic type (<out Type: Any>) or allowing nullable types in the code.

P.S. Please do not use casting ( as Vector<Any> ) in Kotlin just because you have problems with types, it is pretty unsafe :)
what if i use Number type instead and just cast to the correct types via when ... is ... expressions

eg
fun a(v: Number) {}
a(5)
a(5.5)
a(5F)
источник

АО

Алексей Овсянников in Kotlin Start
What are you doing in fun a?
источник

АО

Алексей Овсянников in Kotlin Start
Something like


fun a(v: Number) {
   when (v) {
       is Double -> …
       …
   }
}


?
источник

MG

Matthew Good in Kotlin Start
yea
источник

VM

Vadim Morozov in Kotlin Start
кто подскажет как работает геттер в котлине?
private val authorizationAvailable: Boolean
       get() = currentViewModel?.viewMode != ViewMode.UNAVAILABLE
источник

VM

Vadim Morozov in Kotlin Start
здесь он получается сразу принимает значение или как?
источник

AL

Alexander Levin in Kotlin Start
Vadim Morozov
здесь он получается сразу принимает значение или как?
Нет, будет вычисляться каждый раз.
источник

AL

Alexander Levin in Kotlin Start
Так что если viewMode будет меняться, то может поменяться и значение, которое будет возвращать authorizationAvailable
источник

VM

Vadim Morozov in Kotlin Start
Alexander Levin
Так что если viewMode будет меняться, то может поменяться и значение, которое будет возвращать authorizationAvailable
получается у меня тут прямая зависимость от viewMode
источник