Néstor Díazgranados
02/15/2021, 7:42 PMif ( class is B)
but it is returning falseBilel El Oud
02/15/2021, 8:41 PMigor.wojda
02/15/2021, 11:16 PMigor.wojda
02/15/2021, 11:26 PMLewis Diamond
02/16/2021, 12:56 AMCicero
02/16/2021, 9:18 AMfun fillFieldsForAttribute(attribute: Map<String, Any>): Any? {
when (localVariable) {
is TruParameter -> {
localVariable.update(attribute)
}
is TruVoltage -> {
localVariable.update(attribute)
}
is TruBatteryState -> {
localVariable.update(attribute)
}
}
return localVariable
}
The classes referenced after “is” are all a subclass of “TruAbstract” which contains “update”
what I tried was
(localVariable as TruAbstract).update(attribute)
return localVariable
and this does not work
Do you see any good idea for a refactor here?Big Chungus
02/16/2021, 11:03 AMSinan Gunes
02/16/2021, 11:56 AMmending3
02/16/2021, 12:53 PMgetResource
give me null?
I have a .txt
file in the same directory with MyClass.java
I print the class with this code
println(MyClass::class.java)
it returns me class com.myapp.vendors.lib.MyClass
However when I want to get the txt file with this code:
println(MyClass::class.java.getResource("my.txt"))
it returns me nullMarc Knaup
02/16/2021, 5:22 PMThis class can only be used with the compiler argument ‘-Xopt-in=kotlin.RequiresOptIn’What is this warning for? I can use the class just fine even without that arguments 🤔 I guess the message is just misleading.
OG
02/16/2021, 5:28 PMDevin Fee
02/16/2021, 7:47 PMkotlinOptions.jvmTarget
to JavaVersion.VERSION_1_8
or JavaVersion.VERSION_15
– assuming i’m using Java 15?
• (1.4.20 release notes where support for 15 was added)
• (jvmTarget documentation)
will my code run in an optimized manner – as it can take advantage of newer JDK features, etc?Marc Knaup
02/16/2021, 9:11 PMUser | UserNotFound | NetworkError
instead of a dozen sealed classes all defining the same errors over and over again.Kirill Grouchnikov
02/16/2021, 10:47 PMnkiesel
02/16/2021, 11:52 PMval port = property.getProperty("notification.port").orEmpty().let { if (it.isNotBlank()) it else "7100"}
? Is there something like ``val port = firstContent(property.getProperty("notification.port"), "7100")` ?igor.wojda
02/17/2021, 7:58 AMinternal
by default?Denis
02/17/2021, 6:17 PMDenis
02/17/2021, 6:28 PMsealed class A {
data class X(val m: Int) : A()
data class Y(val m: Int) : A()
data class Z(val m: Int) : A()
}
val x = when (val a: A = A.X(3)) {
is A.X, is A.Y -> a.m // <<<<<<<<<<
is A.Z -> a.m
}
1. Why does Android Studio say 'when' expression must be exhaustive, add necessary 'else' branch
? Aren't all cases covered here?
2. Why is a.m
no accessible on the marked line? It says Unresolved reference: m
.Denis
02/17/2021, 8:00 PMwhen
altogether in this case?
val x: A = A.X(0)
when (x) {
else -> 1
}
Kuba Petržílka
02/17/2021, 8:14 PMequals()
methods in each and every data class when I want to use one of these? 🤔 If so why such comparable variants of the spec. array types are not part of the STL yet?
Obviously, I cannot use all the exension methods provided by the STL directly then.. but it still feels a bit better than messing up my domain model with all those overrides of equals() and hashCode() typically because of one single array field..
Something like this:
class ComparableByteArray (val array: ByteArray) {
val size: Int
get() = array.size
constructor(size: Int) : this(ByteArray(size))
constructor(other: ComparableByteArray) : this(other.array.copyOf())
override fun equals(other: Any?): Boolean =
(this === other) || (javaClass == other?.javaClass && array.contentEquals((other as ComparableByteArray).array))
override fun hashCode(): Int = array.contentHashCode()
override fun toString(): String = array.toString()
operator fun get(index: Int) = array[index]
operator fun set(index: Int, value: Byte) = array.set(index, value)
operator fun iterator(): ByteIterator = array.iterator()
}
data class User {
//....,
secretKey: ComparableByteArray
//...,
// <-- Would have to override equals() and hashCode() here if I used just ByteArray so the data class loses its purpose
}
antonkeks
02/17/2021, 9:19 PMfun <T: Any> T.toValuesSkipping(vararg skip: KProperty1<T, *>): Map<String, Any?> =
(this::class.memberProperties - skip)
.filter { it.javaField != null }.map { it.name to it.get(this) }.toMap()
Dariusz Kuc
02/17/2021, 9:55 PMclass Wrapper {
sealed class Whatever {
abstract val name: String
}
data class First(override val name: String, val first: String) : Whatever() // constructor is private....
}
Gyu hyeon Lee
02/18/2021, 7:17 PM@Cacheable
method return value when it clearly should be working since I literally just put the same class instance into the cache when I called the method for the first time 1 second ago...
Couldn't find a single article that helped me on the issue. mvn clean
didn't help either.
... It worked after I invalidated intellij cache and restarted IDE.
I love my life sometimes.Fabio
02/18/2021, 10:06 PMbodiam
02/18/2021, 11:23 PMval product: Product = products.find { it.name = input } ?: throw IllegalArgumentException("Product $input not found")
I know that it will be very unlikely to not find the product in this list (say, impossible), so, I’d rather not clutter my code with the “if null throw exception” stuff, and rather have find
throw an exception if the product can’t be found, like this:
val product: Product = products.find { it.name = input }
I’d rather not make Product nullable (Product?
), cause that will lead to some if/then elses later in my code. Is there any clean way I’ve missed to accomplish this?Manuel Pérez Alcolea
02/19/2021, 5:16 AMjava
, how can I make it run? I've tried stuff like this:
$ java -cp "/usr/share/kotlin/lib/kotlin-stdlib.jar:." kott.AppKt
Error: Could not find or load main class kott.AppKt
Caused by: java.lang.ClassNotFoundException: kott.AppKt
$ java AppKt
Error: Could not find or load main class AppKt
Caused by: java.lang.NoClassDefFoundError: kott/AppKt (wrong name: AppKt)
Slackbot
02/19/2021, 8:36 AMJames Richardson
02/19/2021, 8:46 AMVictor Ermolaev
02/19/2021, 10:44 AMclass User(val id: PublicKey)
but it does fail with generics class<T> User(val id: T)
. I get Class 'RSAPublicKeyImpl' is not registered for polymorphic serialization in the scope of 'PublicKey'.
when I insert a public key instance there.
My minimally working example is here https://github.com/vnermolaev/serde Does anyone have experience with kotlinx.serialization and applying it together with generics?stephanmg
02/19/2021, 10:45 AMstephanmg
02/19/2021, 10:45 AMCicero
02/19/2021, 11:00 AMstephanmg
02/19/2021, 11:16 AMCicero
02/19/2021, 11:59 AMstephanmg
02/19/2021, 12:22 PMCicero
02/19/2021, 12:23 PMstephanmg
02/19/2021, 12:24 PMCicero
02/19/2021, 12:27 PMstephanmg
02/19/2021, 12:28 PMCicero
02/19/2021, 12:30 PMstephanmg
02/19/2021, 12:31 PMCicero
02/19/2021, 12:47 PMstephanmg
02/19/2021, 1:54 PM