zak.taccardi
11/03/2022, 3:36 AMinterface Validatable {
fun validate()
}
class Impl() :
// both these extend `Validatable`
Validatable1 by Validatable1.Impl(),
Validatable2 by Validatable2.Impl(),
Validatable {
override fun validate() {
// how do I call:
// validatable1.validate()
// validatable2.validate()
// or bonus - can I access the `Validatable1.Impl()` instance at all?
}
}
Slackbot
11/03/2022, 12:23 PMPasha Finkelshteyn
11/04/2022, 10:49 AMRuckus
11/04/2022, 3:49 PMvalue.toString()
instead of value?.toString()
. I'm not a fan of Any?.toString()
, and I'd like to just get rid of it locally if possible.elect
11/05/2022, 4:49 AMsealed interface
, Idea's popup says
The feature "sealed interfaces" is only available since language version 1.5then I click it and in my
build.gradle.kts
it adds
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
languageVersion = "1.5"
}
but sealed
is still marked as error and compiling fails:
> Task :buildSrc:compileKotlin FAILED
3 actionable tasks: 1 executed, 2 up-to-date
e: /home/elect/IdeaProjects/vkk/buildSrc/src/main/kotlin/vkk/parsing.kt: (79, 1): The feature "sealed interfaces" is only available since language version 1.5
Oleg Shuliak
11/05/2022, 10:42 AMServiceInterface
that has 2 implementations ConcreteService1
and ConcreteService2
It’ll have one function doWork()
.
I need to use one implementation or another based on some property, that comes as a parameter. And I don’t understand where do I resolve which concrete class to use.
class SomeClass(
val someService: ServiceInterface
) {
fun someFunction(status: String) {
when(status) {
"ACTIVE" -> someService.doWork() // or even better to return the instance of service
"INACTIVE" -> someService.doWork()
}
}
}
Could someone give a hint?Martin Gaens
11/05/2022, 11:28 AMelect
11/06/2022, 9:40 AMcomment
which I'm initializing by delegation
sealed class Type(line: String) {
val comment by line
However there might be some cases where I need to set it explicitly via constructor.. idiomatic code
sealed class Type(line: String, val comment: String = by line)
what's my best option for this?Muhammad Usman
11/07/2022, 5:34 AMType parameter output is declared as 'out' but occurs in 'invariant' position in type Flow<PagingData<output>>
abstract class PaginatedFlowUseCase<in Input, out output>(
private val dispatcher: CoroutineDispatcher
) {
operator fun invoke(input: Input): Flow<PagingData<output>> {
return execute(input).flowOn(dispatcher)
}
protected abstract fun execute(input: Input): Flow<PagingData<output>>
}
But it works when changing PagingData to kolin.Result classRohan Sanap
11/07/2022, 8:01 AMLukasz Kalnik
11/07/2022, 9:35 AMinternal
members of the tested classes (even though they are in the same Gradle module).
Generally I made some changes which currently broke the build, but they are not related to the internal function/properties. So the project indeed should not build, but the error message is definitely a false positive.Zinedine Bedrani
11/07/2022, 12:33 PMobject Libs {
object Google {
…
object Test {
……
}
object Firebase {
…..
}
}
object Accompanist {
……
}
object Gson {
…..
}
object Log {
…….
}
//other objects ...
}
Is it ok to creates many object like this just to handle dependencies ? for me it’s not :(elect
11/07/2022, 1:16 PMContracts are not allowed for operator functionsWhy is that?
y
11/07/2022, 2:07 PMT?
like an optional of type T
?Rodrigo Silva
11/07/2022, 2:09 PMCannot inline bytecode built with JVM target 17 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option
my config
tasks {
test { useJUnitPlatform() }
compileKotlin { kotlinOptions.jvmTarget = "17" }
...
}
What am I doing wrong?Ellen Spertus
11/07/2022, 2:42 PMrequire()
a function rather than a string?
I can think of two possible answers:
1. In case constructing the message is expensive (seems unlikely).
2. In case we want to do additional processing before throwing the exception, such as logging or cleaning up.
I'm teaching about Kotlin exceptions, so I really want to get this right.Ellen Spertus
11/07/2022, 4:04 PMclass WordleGame(val secretWord: String) {
companion object {
private val words = java.io.File("words.txt").readLines()
fun playGame() {
val game = WordleGame(words.random())
...
}
}
}
fun main() {
WordleGame.playGame() // ExceptionInInitializerError
}
Is there a clean way to catch a FileNotFoundException
? I can catch a ExceptionInInitializerError
in main()
and pull out its cause, but I'm hoping for something easier to explain to students, possibly restructuring the code.elect
11/08/2022, 4:07 AMNULL
as a companion object of an inline class using a type parameter?
@JvmInline
value class Ptr<T>(val adr: Adr = 0L) {
companion object {
val NULL: Ptr<*>
get() = Ptr(MemoryUtil.NULL) // Not enough information to infer type variable T
}
}
Michael de Kaste
11/09/2022, 10:48 AMfun main() {
println("%s %s".format("hi", "bye"))
println("%s %s".format(*arrayOf("hi", "bye")))
println("%s %s".format(arrayOf("hi", "bye"))) // java.util.MissingFormatArgumentException: Format specifier '%s'
}
Anyone know why it goes wrong with a pure array declaration?Xad Kile
11/09/2022, 12:51 PMobject MyObject{ }
). In my app, there's a snippet that looks like this
fun myFunction():Any{
try{
// do things
}catch(e:Throwable){
return MyObject.sideEffect()
}
}
object MyObject{
fun sideEffect():Any{
//....
}
}
the code inside the try block will throw an StackOverflowError under certain condition. The StackOverflowError is caught, and a side effect is emitted. This side effect is produced by an object declared in a different file, and half of the time, it will throw this exception; while on the other half, it runs just fine.
Could not initialize class com.example.MyObject
java.lang.NoClassDefFoundError: Could not initialize class com.example.MyObject
However, if I run this val mo = MyObject
before calling myFunction()
, the NoClassDefFoundError
exception is never thrown.
Maybe this has something to do with the fact that MyObject is lazily allocated according to the official document. Could someone point me to a more in-depth document on object declaration? I need to know what is going on badly :melting_face: Thank you.xun su
11/09/2022, 9:27 PMapt
? I'm using ubuntuxun su
11/09/2022, 9:27 PMAlex
11/10/2022, 9:18 AMval nameList:List<String>=listOf("Apple","Orange","Pear")
->> CODE A
2. I get the following error below "Property getter or setter expected"
3. i had to write as following val nameList=listOf<String>("Apple","Orange","Pear")
which is fine with me, but why cant i use Code A, hope someone can advise meelect
11/10/2022, 10:24 AMy
11/10/2022, 12:41 PMsealed class
with many data class
es that subclass it. I get these subclasses from some stream
I want to build some sort of state machine that changes its state based on the last seen subclass. what’s a cheap/easy way to encode/decode the type of the seen class?y
11/10/2022, 12:43 PMsarvagya agarwal
11/10/2022, 4:32 PMvar list = serviceA.call(list)
list = serviceB.call(list)
list = serviceC.call(list)
list = serviceD.call(list)
jeggy
11/10/2022, 5:39 PMjava.lang.NullPointerException: null cannot be cast to non-null type kotlin.Number
at com.apurebase.puredynamic.repository.user.mapping.UserMapping.<init>(UserMapping.kt:69) ~[main/:?]
at com.apurebase.puredynamic.repository.user.mapping.UserMapping.<init>(UserMapping.kt:10) ~[main/:?]
PHondogo
11/10/2022, 8:06 PMxun su
11/10/2022, 8:39 PMpackage com.xun.kotlin
to the file, notice the error beside the build button. what's wrong with that ? if I comment the line, the error disappear.
I used to be a frontend developer, In javascript, there is no need to add the line like this.xun su
11/10/2022, 8:39 PMpackage com.xun.kotlin
to the file, notice the error beside the build button. what's wrong with that ? if I comment the line, the error disappear.
I used to be a frontend developer, In javascript, there is no need to add the line like this.ephemient
11/10/2022, 8:44 PMMainKt
was to the package-less name. Now that you've added a package name, that needs to change.xun su
11/10/2022, 8:59 PMMainKt
, but not Main.kt
?
anyway I removed that line, Yes, now I could build and run the code via IDEA
but how about use command line ?
java -jar path/to/build/libs/result.jar
just show me a error:
then I searched a similar question and get a answer like this
so I add a line to my build.gradle.kts
,:
tasks.withType<Jar> { manifest { attributes["Main-Class"] = "com.xun.kotlin.MainKt" } }
but I got a another error:
xun@xun-Yoga-14sIHU-2021:~/Desktop/kotlin-basic/build/libs$ java -jar kotlin-basic-1.0-SNAPSHOT.jar
Error: Could not find or load main class com.xun.kotlin.MainKt
Caused by: java.lang.ClassNotFoundException: com.xun.kotlin.MainKt
ephemient
11/10/2022, 9:01 PMfile.kt
in a FileKt
class for Java compatibility by default (you can choose a different name with @file:JVM name("...")
)plugins {
application
}
application {
mainClass.set("com.xun.kotlin.MainKt")
}
this will allow you to use ./gradlew run
xun su
11/10/2022, 9:06 PMkotlin-basic-1.0-SNAPSHOT.jar
is in build directory so it has all it's dependencies bundledephemient
11/10/2022, 9:06 PM./gradlew installDist
which will create a directory build/install
containing everything needed, or ./gradlew distZip
or ./gradlew distTar
which will create that bundle in build/distributions
xun su
11/10/2022, 9:08 PMStephan Schroeder
11/11/2022, 8:56 AMephemient
11/11/2022, 9:00 AMxun su
11/11/2022, 10:00 AM./gradlew build
to build the project and then run it from a JAR file as usual (java -jar <JAR file>
)."
I thought the builded jar file is in build/libs/
, so I went there but still got error: