matt tighe
06/12/2020, 9:16 PMwhen (result) {
is SuccessDataClassWithGenericTypeOf<MyGenericType> -> doThings(it)
is Failed -> doFailedThings(it.failureMessage)
is Loading -> doLoadingThings
}
Why does the type get erased in this case:
sealed class MyResultType {
data class SuccessDataClassWithGenericTypeOf<T>(val result: T) : MyResultType()
object Loading : MyResultType()
data class Failed(val failureMessage: String) : MyResultType()
}
But not in this case:
sealed class MyResultType<out T> {
data class SuccessDataClassWithGenericTypeOf<T>(val result: T) : MyResultType<T>()
object Loading : MyResultType<Nothing>()
data class Failed(val failureMessage: String) : MyResultType<Nothing>()
}
Alexey Demedeckiy
06/13/2020, 6:59 PMclass Store {
private val channel = Channel<Action>()
suspend func dispatch(action: Action) {
channel.send(action)
}
init {
GlobalScope.launch {
for (action in channel) {
reduce(action)
notifyObservers()
}
}
}
}
Sadly, this code doesn't work - actions are not reducer.
Small detail: If i will explicitly set Dispatchers.Main in init and in dispatch - it work, but defeats my purpose.
My goal is to move action processing of main thread. notifyObservers
will build Props
for each active ViewModel
and publish.harry.singh
06/17/2020, 2:16 PMmatt tighe
06/17/2020, 9:37 PMsealed class ErrorStates {
object ThingsAreBadNow {
onAccess {
Logger.log("A bad thing happened")
}
}
}
The init
block can be used for data classes. Is there something I can do to get a similar behavior for objects?Tony Blundell
06/18/2020, 8:54 PMclass SecretsManager(region: String) {
private val gson = Gson()
private val smClient = AWSSecretsManagerClientBuilder.standard().withRegion(region).build()
fun <T> getSecret(id: String): T {
val req = GetSecretValueRequest().withSecretId(id)
val json = smClient.getSecretValue(req).getSecretString()
return gson.fromJson(json, T::class.java)
}
}
To be used like this...
val myInstance = SecretsManager("eu-west-2").getSecret<MyDataClass>("myId")
Currently, I get an error Cannot use 'T' as reified type parameter
. I can get around this by marking the function as inline
and T as reified
, but then I can't access the private attributes from within the function.
What's the best way to do this in Kotlin?Wesley Acheson
06/19/2020, 11:36 AMOptional<String>.map(...).orElse(null)
I'm currently writing this if (source.mpi.dsEndProtocolVersion!= null) Version(source.mpi.dsEndProtocolVersion!!) else null
but that doesn't feel right to me.
Nullable String to nullable VersionLqncer
06/21/2020, 3:12 PMJoakim Tengstrand
06/22/2020, 10:26 AMJoakim Tengstrand
06/22/2020, 12:46 PMplugins {
kotlin("jvm") version "1.3.72"
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath(kotlin("gradle-plugin", version = "1.3.72"))
}
}
sourceSets.main {
java.srcDirs("components/logger/src",
"components/user/src")
}
Joakim Tengstrand
06/22/2020, 3:02 PMStefano Longhi
06/24/2020, 12:46 AMbuildConfigField("String", apiKey, "123abc")
but I received this error: “Unresolved reference: buildConfigField ”
All the example that I found put the command in Android{ … } but I’m not doing an android app, only a simple web service.
Do you have any idea ho I could resolve the problem? thanksDella Anjeh
06/24/2020, 6:53 PMSean
06/25/2020, 5:52 PMTerry Franklin
06/28/2020, 11:42 PM{
"$schema": "<http://json-schema.org/draft-07/schema#>",
"$id": "<https://abc.com/request.schema.json>",
"title": "Request record",
"type": "object",
"properties": {
"id": {
"description": "The unique ID of a request",
"type": "string"
},
"name": {
"description": "The name of a request",
"type": "string"
}
},
"required": [
"id"
]
}
I would expect to receive output like -
data class Request(
val id: String,
val name: String?
)
but instead, this is generated (both fields are marked as required) -
data class Request(
val id: String,
val name: String
)
Ideally I'd like to find a solution that can be implemented programatically too, rather than requiring a manual plugin (though that's not as important as getting the nullable part right).Alexandre Marcondes
06/29/2020, 5:29 PMakshay
06/30/2020, 3:57 PMJoe Masilotti
07/01/2020, 6:24 PMli-s
07/03/2020, 4:32 AMdata class Item(val completed: Boolean, val details: String? = null)
and a list:
val p = listOf(
Item(true, "one"),
Item(false, "two"),
Item(true, "three")
)
I need to transform this data (truncate the second value in the Item to return something like:
listOf(
Item(true, "o"),
Item(false, "t"),
Item(true, "t")
)
Im trying:
val p = listOf(
Item(true, "one"),
Item(false, "two"),
Item(true, "three")
)
println(p.map { (_,details) -> details?.take(1) })
but it returns: [o, t, t]
What’s the best way to do this in Kotlin?
Thank you in advancehuehnerlady
07/03/2020, 3:54 PMTony Blundell
07/04/2020, 12:20 AMdata class MyClass(val type: String) {
companion object {
fun fromJson(j: String) = Gson().fromJson(j, MyClass::class.java)
}
}
data class MyOtherClass(val type: String) {
companion object {
fun fromJson(j: String) = Gson().fromJson(j, MyOtherClass::class.java)
}
}
Fedor
07/04/2020, 12:06 PMT.serializer().list
serializer, that does exactly what I expect from List<T>.serializer(), but with this approach it seems like I have to write a custom serializer for any class that contains List in itNishoobansal
07/04/2020, 2:47 PMDan O'Brien
07/05/2020, 3:55 PMERROR - pplication.impl.LaterInvocator - loader constraint violation: when resolving method 'com.intellij.ui.layout.CellBuilder com.intellij.ui.layout.Row.intTextField(kotlin.reflect.KMutableProperty0, java.lang.Integer, kotlin.ranges.IntRange)' the class loader com.intellij.ide.plugins.cl.PluginClassLoader @6ad38805 of the current class, com/github/intheclouddan/intellijpluginld/LaunchDarklyConfigurable$createPanel$$inlined$panel$lambda$4, and the class loader com.intellij.util.lang.UrlClassLoader @215be6bb for the method's defining class, com/intellij/ui/layout/Cell, have different Class objects for the type kotlin/ranges/IntRange used in the signature (com.github.intheclouddan.intellijpluginld.LaunchDarklyConfigurable$createPanel$$inlined$panel$lambda$4 is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @6ad38805, parent loader 'bootstrap'; com.intellij.ui.layout.Cell is in unnamed module of loader com.intellij.util.lang.UrlClassLoader @215be6bb, parent loader 'platform')
I’m trying to use an intellij intTextField
and just assign a var as refreshRate: Int
Hannan Shaikh
07/06/2020, 2:18 PMMark
07/07/2020, 5:52 AMFragment
subclass:
inline fun <T> Flow<T>.collectInViewScope(crossinline action: suspend (value: T) -> Unit) {
viewLifecycleOwner.lifecycleScope.launch {
collect(action)
}
}
But is there a way to declare this outside of the class (so it can be used from any Fragment
subclass, without having to pass in the Fragment
instance as an arg? A kind of extension function with added context of the call site. One could imagine inline <http://Fragment.fun|Fragment.fun> <T> Flow<T>.collectInViewScope(…)
Sam Smallman
07/07/2020, 2:13 PMGus
07/07/2020, 4:21 PMObject
). What's the best / most idiomatic way to represent that collection? In TypeScript I'd do something like Array<Type1 | Type2>
, but I don't know how to do something like that on Kotlin/Java/JVM.Mark
07/08/2020, 5:13 AMclass Settings(val sharedPreferences: SharedPreferences) {
val appVersion: Int by <http://sharedPreferences.int|sharedPreferences.int>(APP_VERSION)
}
but I would like to do:
val Settings.appVersion: Int by <http://sharedPreferences.int|sharedPreferences.int>(APP_VERSION)
Cheolho Jeon
07/08/2020, 8:50 AMhorse_badorties
07/09/2020, 6:37 AMhorse_badorties
07/09/2020, 6:37 AMMatteo Mirk
07/09/2020, 8:53 AMHaomin
07/09/2020, 5:32 PM