Dominick
04/23/2021, 1:57 AM<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
I don't understand why I get this error. If anyone has insight or a fix please share!dave08
04/23/2021, 9:17 AMspand
04/23/2021, 1:03 PMallWarningsAsErrors = true
for all my modules regardless of them being, common, multiplatform-js, multiplatform-jvm, kotlinjs, kotlin ect. Is this the right type to search for ?
subprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.class) {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
Slackbot
04/23/2021, 5:11 PMDominaezzz
04/24/2021, 9:19 AMval myNum: Int = 5
val myBigNum: Long = myNum // fine
val myMaybeBigNum: Long? = myNum // not fine
F0X
04/24/2021, 9:37 AM0xFF
and have that be converted to a signed Byte
Yaniv Sosnovsky
04/24/2021, 3:11 PM{"drinks":[{"strDrink":"Jelly Bean","strDrinkThumb":"https:\/\/<http://www.thecocktaildb.com|www.thecocktaildb.com>\/images\/media\/drink\/bglc6y1504388797.jpg","idDrink":"13775"},{"strDrink":"Turf Cocktail","strDrinkThumb":"https:\/\/<http://www.thecocktaildb.com|www.thecocktaildb.com>\/images\/media\/drink\/utypqq1441554367.jpg","idDrink":"12418"}]}
but sometimes the server returns the following:
"{"drinks":"None Found"}"
Before using kotlinx I had a custom type convertor with Gson, and that worked fine, but I’m struggling with kotlin.
I tried to create a KSerializer to try to parse the result, and return an object with an empty list if an exception is thrown, but I keep getting this exception:
java.lang.IllegalStateException: Reader has not consumed the whole input: JsonReader(source='{"drinks":"None Found"}', currentPosition=22, tokenClass=1, tokenPosition=10, offset=11)
Any ideas how to solve this? I can always return a string instead of a data class, but this is ugly AFMichael
04/26/2021, 7:29 AMuser
04/26/2021, 8:40 AMStephan Schroeder
04/26/2021, 11:27 AMalgo112
04/26/2021, 1:40 PMval listType = object : TypeToken<List<Cookie>>() {}.type
val encodedCookie = sharedPreferences.getString(url.host, null)
val cookies: List<Cookie>? = encodedCookie?.run { gson.fromJson(encodedCookie, listType) }
return cookies ?: ArrayList()
stanislav.erokhin
04/26/2021, 3:06 PMursus
04/26/2021, 5:19 PMDavid Smith
04/26/2021, 8:52 PMBig Chungus
04/26/2021, 10:11 PMursus
04/26/2021, 11:24 PM@OptIn
?ursus
04/27/2021, 2:19 AMe: org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during psi2ir
File being compiled: C:/Users/ursus/AndroidStudioProjects/o2-selfcare-android/complex-syncer/contract/src/main/java/sk/o2/complex/Mappers.kt
The root cause java.lang.NullPointerException was thrown at: org.jetbrains.kotlin.psi2ir.generators.BodyGenerator.generateSuperConstructorCall(BodyGenerator.kt:242)
at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:235)
at org.jetbrains.kotlin.psi2ir.generators.DeclarationGenerator.generateMemberDeclaration(DeclarationGenerator.kt:78)
at org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator.generateSingleFile(ModuleGenerator.kt:83)
at org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator.generateModuleFragment(ModuleGenerator.kt:50)
at org.jetbrains.kotlin.psi2ir.Psi2IrTranslator.generateModuleFragment(Psi2IrTranslator.kt:81)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:140)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr$default(JvmIrCodegenFactory.kt:66)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModule(JvmIrCodegenFactory.kt:61)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:592)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:212)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:155)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:88)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:386)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:110)
Muhammad Zaryab Rafique
04/27/2021, 4:28 AMMichael Böiers
04/27/2021, 5:58 AMdata class A(var b: B? = null)
data class B(var a: A? = null)
fun main() {
val a = A()
val b = B(a).also { a.b = it }
println(a) // produces StackOverflow as the two toString impls call each other indefinitely
}
I suppose this is unavoidable - nevertheless, it can kill an application merely by converting a plain Java class into a data class. Any thoughts?PHondogo
04/27/2021, 9:47 AMthanksforallthefish
04/27/2021, 12:40 PMKarlo Lozovina
04/27/2021, 3:19 PMlatinit
properties after they've been initialized? Something in the stdlib maybe?Quincy
04/27/2021, 7:34 PMStream.of(...)
.toMap(
keySelector,
valueTransform,
(x, y) -> {
if (x.getTimestamp().isBefore(y.getTimestamp())) return x;
else return y;
});
If there would be duplicate values then the third argument determines what to do about it. In Kotlin, the associateBy
function always keeps the most recent value. Is there a way to create a map the way Java does, where I get to decide what to do with duplicates?Karlo Lozovina
04/27/2021, 7:46 PMblakelee
04/27/2021, 10:08 PMenum class Page {
PAGE_1,
PAGE_2;
}
enum class Page1Section {
SECTION_A,
SECTION_B
}
enum class Page2Section {
SECTION_1,
SECTION_2
}
With my end goal being something like
Page.PAGE_1.SECTION_A.build()
and if I selected PAGE_2 then
Page.PAGE_2.SECTION_1.build()
Maybe this would be better in sealed classes
If I selected PAGE_1, if I were to type .
then I would get the list of Page1Section's then if I click .
again perhaps another list of itemsChuck Canning
04/27/2021, 11:13 PMDavid Smith
04/28/2021, 12:42 AMhashCode()
already but I want to avoid having to call it as a function because during a diff of a deeply nested structure this could happen many many times, i.e. I want to memoize the call. Can I simply add a property that is initialized to this.hashCode()
? I’ve implemented the following which seems to work (although I haven’t tested it properly yet)
sealed class Node {
data class TextNode(val value: String) : Node() {
override val hash = this.hashCode()
}
data class TagNode(val name: String, val attributes: List<Attribute>, val children: List<Node>) : Node() {
override val hash = this.hashCode()
}
abstract val hash: Int
}
amr s
04/28/2021, 2:39 AMCLOVIS
04/28/2021, 7:47 AMtailrec suspend
to work, but it does! Thanks to the team, that must have been a weird edge case to handle.jim
04/28/2021, 6:14 PMit.
?
kotlin
val a = "1 2\n3 4".lines().map{CharSequence::split("\\s+".toRegex())}
log
Matrix.kt: (10, 59): This syntax is reserved for future use; to call a reference, enclose it in parentheses: (foo::bar)(args)
e: /home/a/github/exercism/kotlin/matrix/src/main/kotlin/Matrix.kt: (10, 54): Overload resolution ambiguity:
public fun CharSequence.split(regex: Pattern, limit: Int = ...): List<String> defined in kotlin.text
public fun CharSequence.split(vararg delimiters: String, ignoreCase: Boolean = ..., limit: Int = ...): List<String> defined in kotlin.text
public fun CharSequence.split(vararg delimiters: Char, ignoreCase: Boolean = ..., limit: Int = ...): List<String> defined in kotlin.text
@InlineOnly public inline fun CharSequence.split(regex: Regex, limit: Int = ...): List<String> defined in kotlin.text
jim
04/28/2021, 6:14 PMit.
?
kotlin
val a = "1 2\n3 4".lines().map{CharSequence::split("\\s+".toRegex())}
log
Matrix.kt: (10, 59): This syntax is reserved for future use; to call a reference, enclose it in parentheses: (foo::bar)(args)
e: /home/a/github/exercism/kotlin/matrix/src/main/kotlin/Matrix.kt: (10, 54): Overload resolution ambiguity:
public fun CharSequence.split(regex: Pattern, limit: Int = ...): List<String> defined in kotlin.text
public fun CharSequence.split(vararg delimiters: String, ignoreCase: Boolean = ..., limit: Int = ...): List<String> defined in kotlin.text
public fun CharSequence.split(vararg delimiters: Char, ignoreCase: Boolean = ..., limit: Int = ...): List<String> defined in kotlin.text
@InlineOnly public inline fun CharSequence.split(regex: Regex, limit: Int = ...): List<String> defined in kotlin.text
nanodeath
04/28/2021, 6:15 PMjim
04/28/2021, 6:21 PMval a = "1 2\n3 4".lines().map(CharSequence::split("\\s+".toRegex()))
isn't working either.fun scoreWord(word: String): Int {
if(word.isEmpty()) return 0
return word
.map(Character::toUpperCase)
.map { scores.getOrDefault(it, 0) }
.reduce(Int::plus)
}
nanodeath
04/28/2021, 6:26 PMjim
04/28/2021, 6:28 PMCharacter::toUpperCase
Class::method
?nanodeath
04/28/2021, 6:29 PM(
I added isn't a typo.map(Character::toUpperCase)
is exactly equivalent to .map { it.toUpperCase() }
or, more verbosely, .map { c: Character -> c.toUpperCase() }
val spaces = "\\s+".toRegex()
val a = "blah".lines().map { it.split(spaces) }
jim
04/28/2021, 6:32 PMmap{it.split("\\s+".toRegex())}
.nanodeath
04/28/2021, 6:37 PM(Character -> T)
instead of a String, you could probably say map(it.split("\s"))
split
accepts one parameter, and that parameter isn't the string being split; it's the pattern you want to split on, which is crucialQuincy
04/28/2021, 6:57 PMval spaces = "\\s+".toRegex()
val spliter = { s: String -> s.split(spaces) }
val a = "blah".lines().map(splitter)
But if the actual use case is not more complicated than what you've shown it's not really buying you anything.jim
04/28/2021, 6:59 PM.map(Character::toUpperCase)
gets translated to .map { c: Character -> c.toUpperCase() }
, why .map(CharSequence::split("\\s+".toRegex())
is not translated to .map{cs:CharSequence -> cs.split("\\s+".toRegex())}
. I just mark it as one parameter extra. Does not work. Thank you for the replies.nanodeath
04/28/2021, 6:59 PMsplitter
thing into an actual method; that might be more efficientQuincy
04/28/2021, 7:02 PMCharacter::toUppercase
is a way of referring to a specific function defined elsewhere which satisfies the type (Character) -> Character
Roukanken
04/28/2021, 7:22 PMmySplit
? ... well original split has like 4 overloads and compiler complains it can't chose. Afaik, doing this is stretching the limits of Kotlin's type system
anyways, to real question: Map can only handle if you pass it reference to function taking 1 argument - because it has 1 value (the it
) and can just chunk it there. But split needs at least 2 arguments - a.split(b)
both a
and b
are arguments, and map
only has 1 value.
And no, you can't just pass 1 argument to reference and expect a new function to be created (sadly)Nir
04/28/2021, 7:30 PMRoukanken
04/28/2021, 7:31 PMit
then easy solution: name itNir
04/28/2021, 7:31 PM"blah".lines().map { it.split("\\s+".toRegex()) }
Already works and is perfectly readable, the problem is that you'd be executing the toRegex over and overspaces
variable but it's not clear how you'd hope to avoid that in general.Roukanken
04/28/2021, 7:42 PMpartial
toRegex
over and over - it passes the function just once to map, with already "inlined" argument
but without managing to resolve that overload ambiguity, it's not really viable solutionNir
04/28/2021, 8:02 PMval a = "1 2\n3 4"
.lines()
.map("\\s+".toRegex().let{ {line -> line.split(it)} })
wbertan
04/28/2021, 9:43 PMRegex
already has a split
. Can check if the output is desirable, but could try this:
val a = "1 2\n3 4".lines().map("\\s+".toRegex()::split)
The docs says:
Splits the input CharSequence around matches of this regular expression.So I believe it should work as expected.