Jérôme Gully
02/07/2020, 1:51 PMif (data in selectedItems) {
selectedItems.remove(data)
} else {
selectedItems.add(data)
}
It is a better way to write this type of condition in Kotlin ?
selectedItems
is an arraylist and data
is an object of arraylist typeKroppeb
02/07/2020, 4:27 PMNothing
at compile time?Fleshgrinder
02/07/2020, 4:46 PM@JvmSynthetic
to hide Kotlin functions from Java users, however, we have no equivalent @JvmOnly
(or whatever it would be named) to hide definitions from Kotlin. I tried @Deprecated("Java only", level = HIDDEN)
but it's hidden in Java afterwards as well if you use IntelliJ. 😛 Any known workarounds for this or is a feature request the only possible thing?fcosta
02/07/2020, 5:06 PMgbaldeck
02/07/2020, 6:10 PMkyleg
02/07/2020, 8:46 PMTypeMismatch
in the onCreateViewHolder
for both branches of the when
?
Return type is listed as the sealed class BaseHolder<PossibleRow>
and the return types are both subclasses of the sealed class.
sealed class PossibleRow { /* objects A, B defined here */ }
sealed class BaseHolder<T: PossibleRow>(...): RecyclerView.ViewHolder(...) {
class AHolder(...): BaseHolder<PossibleRow.A>(...)
class BHolder(...): BaseHolder<PossibleRow.B>(...)
}
class LiftGroupRowAdapter {
override fun onCreateViewHolder(..., viewType: Int): BaseHolder<PossibleRow> {
return when(viewType) {
0 -> BaseHolder.AHolder(...)
1 -> BaseHolder.BHolder(...)
else -> throw IllegalStateException("invalid viewType")
}
}
}
Sinan Kozak
02/08/2020, 6:08 AMTmpod
02/08/2020, 2:01 PMribesg
02/08/2020, 7:43 PMjavaClass.getResourceAsStream
returns null, any idea? I can see the file in the jar when I open it, I don't know what's happeningSaood
02/09/2020, 11:06 AMSlackbot
02/09/2020, 12:22 PMYan Pujante
02/09/2020, 1:44 PMAnimesh Sahu
02/09/2020, 2:32 PMChills
02/09/2020, 3:57 PMRodrigo Silva
02/09/2020, 4:07 PMbod
02/09/2020, 5:09 PMBruno_
02/09/2020, 6:12 PMken
02/09/2020, 10:27 PMGyuhyeon
02/10/2020, 5:24 AM/* Case A: java-style */
val result = repository.select()
if (result != null) {
doSomething() // business logic
} else {
doSomethingElse() // business logic
}
/* Case B: Kotlin style...? */
repository.select()?.also {
doSomething()
} ?: run {
doSomethingElse()
}
/* Case C: Kotlin style... + better job security through code only the creater can understand? */
try {
repository.select()!!.run {
doSomething()
}
} catch (NullPointerException e) { // or whatever it was that catches not null assertion error
doSomethingElse()
}
What is the better solution here? Obviously we can't make repository.select return non nullable type because that will result in an exception anyway. Business logic requires a different action for nonexisting row, and the only way to do this is either catch an exception, use obscure code with .also
and ?:
, or use plain old java style code. Is there any other solution to this?camkadev
02/10/2020, 12:31 PMkotlinx-serialization
example for protobuf
on jvm
?Madhan
02/10/2020, 12:49 PMRob Murdock
02/10/2020, 3:30 PMbbaldino
02/10/2020, 5:05 PMNonZeroInt
. I can do something like throw in plus
, minus
, etc. if the result ends up being 0...but how can I prevent the instance from being initialized with 0? I can't do an init block to verify it, and have to expose the primary constructor. I tried looking at UInt
, which prevents you from assigning a negative number to it, but how does it enforce that? The primary constructor takes in an Int
.Pablo Schmid
02/10/2020, 6:01 PMcak
02/11/2020, 2:31 AMaeruhxi
02/11/2020, 6:19 AMJakub
02/11/2020, 9:19 AMlist.add(item)
everywhere.
(I will use simple example)
I’ve tried to replace
(0..3).forEach {
list.add(it)
}
with
list.add(
(0..3).forEach {
it
}
)
However, it doesn’t compile. Is there a way to achieve it?nitrog42
02/11/2020, 2:03 PMEllen Spertus
02/11/2020, 7:11 PMString.replace()
or String.format()
, or is there a more elegant solution in Kotlin than there would be in Java?Martin Nordholts
02/12/2020, 8:54 AMnull TheValue
? I expect it to print TheValue TheValue
enum class EnumClass(val self: EnumClass? = null) {
TheValue(self = TheValue)
}
enum class OtherEnumClass(val other: EnumClass? = null) {
OtherValue(other = EnumClass.TheValue)
}
fun main() {
println("${EnumClass.TheValue.self} ${OtherEnumClass.OtherValue.other}")
}
Martin Nordholts
02/12/2020, 8:54 AMnull TheValue
? I expect it to print TheValue TheValue
enum class EnumClass(val self: EnumClass? = null) {
TheValue(self = TheValue)
}
enum class OtherEnumClass(val other: EnumClass? = null) {
OtherValue(other = EnumClass.TheValue)
}
fun main() {
println("${EnumClass.TheValue.self} ${OtherEnumClass.OtherValue.other}")
}
spand
02/12/2020, 8:56 AMMartin Nordholts
02/12/2020, 8:58 AMdiesieben07
02/12/2020, 9:00 AMTheValue
constructor does not run when you print, it runs at the class-init time of EnumClass
, before EnumClass.TheValue
has been assigned (how can it be assigned, you are in the middle of creating it's value!)spand
02/12/2020, 9:01 AMTheValue(self = TheValue)
TheValue has not been thus self is assigned to nullMartin Nordholts
02/12/2020, 9:01 AMStephan Schroeder
02/12/2020, 10:11 AMby lazy {..}
to compute values that aren’t available immediately).
It pretty much took extra effort (nullable class + default null parameter value) to not make the compiler complain about this 😂Martin Nordholts
02/12/2020, 10:12 AMStephan Schroeder
02/12/2020, 10:14 AMnull
to start with.Martin Nordholts
02/12/2020, 10:16 AMexactly what you defindedI don’t see how you can think that
TheValue(self = TheValue)
is to be interpreted
TheValue(self = null)
spand
02/12/2020, 10:30 AMMike
02/12/2020, 12:36 PMself
in a class to the class itself. But in order to assign it, the class has to be constructed.
But in order for the class to be constructed, it needs the pointer to the class.
etc, etc, etc...
So I'm unsure how you expect it to work.
And the above also explains why it wouldn't have compiled if self
was non-null. But just getting it to compile doesn't always guarantee it will work. We all wish it was that simple 😉Martin Nordholts
02/12/2020, 12:40 PMenum class EnumClass(var self: EnumClass? = null) {
TheValue();
}
enum class OtherEnumClass(val other: EnumClass? = null) {
OtherValue(other = EnumClass.TheValue)
}
fun main() {
EnumClass.TheValue.self = EnumClass.TheValue
println("${EnumClass.TheValue.self} ${OtherEnumClass.OtherValue.other}")
}
that code runs just fine.diesieben07
02/12/2020, 12:42 PMTheValue
at the point where you construct TheValue
, in the initializer of EnumClass
.Martin Nordholts
02/12/2020, 12:44 PMdiesieben07
02/12/2020, 12:46 PMMartin Nordholts
02/12/2020, 12:46 PMMike
02/12/2020, 12:47 PMTheValue
object gets created as soon as EnumClass.TheValue
is executed. This will initialize the object, assign it in memory. And since self
is nullable AND has a default value, all is good.
Now the compiler will execute the assignment of self
within TheValue
, and it has an object to point to. This works ONLY because enums are singletons.
When you try to combine it, you put the compiler into a stackoverflow situation as described above.
Hopefully that helps you conceptualize it.Martin Nordholts
02/12/2020, 12:50 PMMike
02/12/2020, 1:57 PMilya.gorbunov
02/12/2020, 10:18 PM