Joan Colmenero
03/23/2020, 3:31 PMsealed class
to use it as a parent?
sealed class ClientName {
data class normalClass(val name: String) : ClientName()
object SpecificObject : ClientName()
object AnotherObject : ClientName()
}
So now, I need to have the same objects and class but different output, so on my extension function :
That is something like this :
fun ClientName.toText(): String = when (this) {
is ClientName.SpecificObject -> "Print1"
is ClientName.AnotherObject -> "Print2"
is ClientName.normalClass -> if (this.name == "") "hello" else "Bye"
}
So now I see that I'd need different types, it means that SpecificObject
, AnotherObject
and normalClass
are goinna print different things depends of the type, so what I thought is create another sealed class
like :
sealed class Type1 : ClientName()
sealed class Type2 : ClientName()
And then in that extension toText
check if is Type1
then do X and if it's Type2
do Y.
But I do not know how to do it to get the same values and same clases as the ClientName
what I'm missing?jimn
03/23/2020, 5:10 PMif (seekTo in (window1.first..(window1.second - rowsize)))
does this compile to something other than it > min and it < max
??Stephan Schroeder
03/23/2020, 6:25 PMkoral
03/23/2020, 9:17 PMtrue
. However, ClassCastException: java.lang.Long cannot be cast to DogId
is thrown.
Is it a bug in kotlin? Or maybe I’m using inline classes incorrectly?J6ey
03/23/2020, 11:48 PMTravis Griggs
03/23/2020, 11:56 PM@ExperimentalStdlibApi
to a bunch of methods, I changed my Kotlin Compiler Preferences in AndroidStudio to include -Xopt-in=kotlin.ExperimentalStdlibApi
in the “Additional command line parameters” (as per the instructions here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-experimental-stdlib-api/). However, I’m still getting build failures when I remove the annotations and depend on the global command line parameter. What am I not getting?Chills
03/24/2020, 9:41 AMthana
03/24/2020, 1:53 PMtasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
freeCompilerArgs += "-Xopt-in=kotlinx.serialization.ImplicitReflectionSerializer"
jvmTarget = "1.8"
}
}
results in
w: Flag is not supported by this version of the compiler: -Xopt=in=kotlinx.serialization.ImplicitReflectionSerializer
?
(kotlin 1.3.61)Dougrinch
03/24/2020, 2:53 PMtypealias F<T> = (T) -> T
fun test() {
val id: F<*> = { it }
consume(id)//error here
}
fun <T> consume(v: F<T>) {
}
It looks like all should be fine, but the compiler tells me Type mismatch: inferred type is Any? but CapturedType(*) was expected
. I thought that the star means "some unknown T", am I wrong?LastExceed
03/24/2020, 3:18 PMGilberto Diaz
03/24/2020, 3:23 PMChills
03/24/2020, 4:02 PMGilberto Diaz
03/24/2020, 5:01 PMsimtse
03/24/2020, 7:27 PM@javax.inject.Singleton
class SpecificWork @Inject constructor() {
private var cruncher: Cruncher? = null
@GuardedBy("this")
init {
initCruncher()
}
@Synchronized
private initCruncher() {
// io work initialization
cruncher = try {
IOCruncher.init()
} catch (e: IOException) {
null
}
}
fun doWork(): Result? {
if (cruncher == null) {
return null
}
// do work
return Result("done")
}
}
This SpecificWorker
is provided via Dagger for dependency injection, but there could be many threads calling into it at the same time. I’m wondering
• Is there a case where the doWork()
started executing before the initCruncher()
was able to assign the cruncher
? Making the cuncher
null and failing early?am414
03/24/2020, 8:42 PMSerhiy Fedusov
03/25/2020, 7:48 AMinline fun <T> Iterable<T>.filter(
predicate: (T) -> Boolean
) : List<T>
If I chain multiple functions, say, using Set, it is simetimes uncomfortable to call toSet()
each time the list is returned.corneil
03/25/2020, 8:03 AMchristophsturm
03/25/2020, 11:34 AMSrSouza
03/25/2020, 3:16 PMEdoardo Luppi
03/25/2020, 5:03 PMlanguage-version
compiler option works? Does it modify the outputted bytecode & metadata to be compatible with another version?amadeu01
03/25/2020, 6:37 PMT::class.java.newInstance() // Where T is a generic type
But, if I want to make an new object that need to receive an argument.
The usage of this would be in a recycler view where I have different view holders but each one has a constructor with only one argument. like
inner class ShortTextViewHolder(
private val viewBinding: ItemRowFormFieldTextBinding
) : RecyclerView.ViewHolder(viewBinding.root), FieldViewHolder {
override fun configureWith(field: FormField) = viewBinding.run {
with(field) {
textFieldName.text = label
textFieldDescription.text = description
}
}
}
So basically, I would like to simplify this
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = ItemRowFormFieldTextBinding.inflate(parent.inflater)
if (viewType == ITEM_TYPE_SHORT_TEXT) {
return ShortTextViewHolder(view)
}
return LongTextViewHolder(view)
}
By calling a Factory like factory<LongTextViewHolder>(view)
or factory(view, myPossibleTypes[viewType])
where myPossibleTypes
would be a list of the types I can instantiatekushalp
03/25/2020, 8:53 PMdata class Sample(val foo: Number)
and the following example map that I may not know until runtime mapOf("foo" to 1, "bar" to 2, "baz" to 3)
. How can I extract just the value that's relevant to the Sample
data class and build it when I don't know if the field in question will be present?Miguel Vargas
03/25/2020, 10:59 PMsuspend FlowCollector<T>.() -> Unit
In general what is the meaning of Type.()?Ellen Spertus
03/26/2020, 12:13 AMval pairs = listOf(
Pair("A", 1),
Pair("A", 2),
Pair("B", 3)
)
to this map:
mapOf(
"A" to listOf(1, 2),
"B" to listOf(3)
)
Burkhard
03/26/2020, 9:40 AMIterable.sum()
that multiplies instead? I know I can use reduce to get the same effect, just looking for a cleaner way to code this.thana
03/26/2020, 12:55 PMSequence
not Iterable
?Andreas Unterweger
03/26/2020, 1:23 PMcase 1: { data: {props.....} } -> StoredVehicleUserSingle
case 2: { data: [ {props.....}, {props.....} ] } -> StoreVehicleUsers with a list of StoredVehicleUsersList
so far i have solved it like this:
data class StoredVehicleUsers(
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, defaultImpl = StoredVehicleUserList::class)
@JsonProperty("data") val vehicleUsers: List<StoredVehicleUser>
)
/**
* we need to implementations for this for:
* [StoredVehicleUsers] its [StoredVehicleUserList]
* if its a single [StoredVehicleUser] we receive its [StoredVehicleUserSingle]
*/
interface StoredVehicleUser {
val vehicleId: String
val userId: String
val role: String?val roleTimestamp: Date?
val resetRole: String?
val resetRoleTimestamp: Date?
}
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName(value = "data")
internal data class StoredVehicleUserSingle(
override val vehicleId: String,
override val userId: String,
override val role: String? = null,
override val roleTimestamp: Date? = null,
override val resetRole: String? = null,
override val resetRoleTimestamp: Date? = null
) : StoredVehicleUser
internal data class StoredVehicleUserList(
override val vehicleId: String,
override val userId: String,
override val role: String? = null,
override val roleTimestamp: Date? = null,
override val resetRole: String? = null,
override val resetRoleTimestamp: Date? = null
) : StoredVehicleUser
i think the solution is fine, the only thing that bugs me, is that i am repeating all properties in StoredVehicleUser, StoredVehicleUserSingle and StoredVehicleUserList, despite they are same. the implementations must be data classes to have euqal and hashcode.
but if i use inheritance i actually must list all props in subclasses again and pass them to Super constructor. also a data class must have at least one constructor argument
How can i do this better? (edited)hooliooo
03/26/2020, 3:14 PMmp
03/26/2020, 4:35 PMtony
03/26/2020, 11:49 PM--release 8
directly. My question is, with a Kotlin-only project, is there a way to tell it that in a gradle build script? I looked in KotlinJvmOptions
and all its super classes, and didn't see any likely suspects. I tried passing it to freeCompilerArgs
, but that complains its an unknown value.tony
03/26/2020, 11:49 PM--release 8
directly. My question is, with a Kotlin-only project, is there a way to tell it that in a gradle build script? I looked in KotlinJvmOptions
and all its super classes, and didn't see any likely suspects. I tried passing it to freeCompilerArgs
, but that complains its an unknown value.danny
03/27/2020, 12:56 AMkotlinOptions.jvmTarget
on the compile tasktony
03/27/2020, 4:50 AMkotlinOptions.jvmTarget
use -target/-source
or --release
when writing java bytecode?Fleshgrinder
03/27/2020, 4:54 PMjvmTarget
to 8
will produce bytecode that is compatible with any JVM release that can interpret it, that's it.tony
03/27/2020, 5:17 PMilya.gorbunov
03/27/2020, 5:56 PMkotlinOptions.jdkHome
parameter in addition to jvmTarget
. This will ensure that you don't accidentaly use any API from a newer JDK.tony
03/27/2020, 5:58 PM