Michael de Kaste
01/05/2022, 4:09 PMfun main(){
val f1: Int = 3
val f2: String = "foo"
val f3: Double = 23.2
val f4: String = "bar"
val f5: String? = "rez"
val f6: String? = null
val model = DBModelRepository.getByField1(f1)
?.apply {
field2 = f2 //need to doubly write all these mandatory fields
field3 = f3 //
field4 = f4 //
} ?: DBModel(
field1 = f1, //
field2 = f2, //
field3 = f3, //
field4 = f4, //can become a big hassle when its 15+ fields
)
model.apply {
field5 = f5 //I can at least save a bit of hassle by doing the optional fields after both cases
field6 = f6
}
DBModelRepository.save(model)
}
class DBModel(
val field1: Int,
var field2: String,
var field3: Double,
var field4: String,
var field5: String? = null,
var field6: String? = null
)
object DBModelRepository{
fun getByField1(field1: Int) : DBModel? {
TODO()
}
fun save(dbMobel: DBModel){}
}
Now is this a relatively "small example" with 4 fields, but we're working with a lot of pretty big models. Sometimes its a bit harder than just "updating" a model like the example and we need to redo this logic in other places. Is there a better way to do this?dany giguere
01/05/2022, 6:33 PMFred Bowker
01/06/2022, 9:34 AMEivind
01/06/2022, 10:19 AMFlorian Magin
01/06/2022, 12:11 PMJason5lee
01/06/2022, 1:54 PMthrow
is unreachable. But after I removed it there is an compile error. Is it a known issue?Jasin Colegrove
01/06/2022, 5:22 PMaddparagraph("") it requires the {}
is there a way around that?Waqas Tahir
01/06/2022, 8:49 PMmatchesList
)
Code With Pattern Class
val matcher = pattern.matcher(string)
val matchesList = mutableListOf<String>()
while (matcher.find()) {
matchesList.add(matcher.group(0))
if (!isGlobal) {
var i : Int = 1
val iEnd : Int = matcher.groupCount()
while (i <= iEnd) {
matchesList.add(matcher.group(i))
i++
}
}
}
return matchesList
Code With Regex Class (not working)
var result : MatchResult? = regex.matchEntire(string) ?: return mutableListOf()
val matchesList = mutableListOf<String>()
while(result!=null){
result.groups[0]?.value?.let { matchesList.add(it) }
if(!isGlobal){
var i = 1
while(i <= result.groups.size){
result.groups[i]?.value?.let { matchesList.add(it) }
i++
}
}
result = result.next()
}
return matchesList
Jan Gerritsen
01/07/2022, 2:22 PMclass Test(
@field:Valid
val inlinedMember: InlinedMember,
@field:Valid
val normalMember: NormalMember,
)
@JvmInline
value class InlinedMember constructor(
@field:NotBlank
val subAttr: String
)
class NormalMember constructor(
@field:NotBlank
val subAttr: String
)
After I compile this code and run it, the property inlinedMember of the class Test is replaces with the type String and the annotation @field:NotBlank is lost, resulting in the validator not validating this constrain.
Has anyone an idea how to keep the information (without copying it) or what else to do?
The goal is to use the same value class (e.g. email, bithdate, username,..) at several places and only define the constrains once.
This is a spring boot project, using jackson to parse and validate the JSON data on the REST controllerShalom Halbert
01/07/2022, 5:53 PMfrank
01/07/2022, 6:35 PMinline fun <reified T> getCollection(collection: String): MongoCollection<T> {
return database.getCollection(collection, T::class.java)
}
Full Code:
open class Connection<T> {
constructor(){
startMongoDB()
}
val client by lazy { KMongo.createClient() }
val database by lazy { getDB() }
fun getDB(): MongoDatabase {
return client.getDatabase(DB_NAME)
}
inline fun <reified T> getCollection(collection: String): MongoCollection<T> {
return database.getCollection(collection, T::class.java)
}
private fun startMongoDB() {
client
database
}
}
Jasin Colegrove
01/07/2022, 7:02 PMval pdfDoc = PdfDocument(PdfWriter(fileName))
but the implementation for PdfDocument is
public PdfDocument(PdfWriter writer) {
this(writer, new DocumentProperties());
}
what is pdfDoc assigned to with no return value?Kirill Grouchnikov
01/07/2022, 9:49 PMClass.forName
+ Class.getConstructor
+ Constructor.newInstance
, but maybe something that starts with KClass
?Jan
01/08/2022, 12:39 PMColton Idle
01/08/2022, 1:17 PMKenneth
01/10/2022, 3:03 PM(x != null && y == null) || (x == null && y != null)
?Mattia Tommasone
01/10/2022, 10:05 PMNikola Milovic
01/11/2022, 1:19 PMimport kotlinx.serialization.Serializable
@Serializable
data class MessageModel (val senderId : String, val channelId : String, val id : String, val contents : String, val date : Long)
And when I do this inside my (this is a multiplatform project) web app
console.log(message.date::class.simpleName )
The result is Double
!! What is happening here? It's causing me runtime errors, as I need a long. This MessageModel I receive from my backend and the value of the date is epoch milis, and the value is 1641905913211
this is interpreted as double. But if I don't use the value of message.date
and instead just directly input 1641905913211
it works, meaning the value isn't the issue, but for some reason when serializing it becomes a double? Not sure where to ask this, is this Multiplatform issue, Kotlin general issue?
const val kotlin = "1.6.10"
const val kotlinxSerialization = "1.3.2"
James Hendry
01/11/2022, 3:13 PMAyfri
01/11/2022, 6:51 PMval List<Int>.minmaxDifference get() = maxOf { it } - minOf { it }
// chestRoom is a set, blockX/Y/Z are Int
// CHEST_ROOM_MIN_SIZE & MAX_SIZE are const val Int
if (chestRoom.map { it.blockX }.minmaxDifference in CHEST_ROOM_MIN_SIZE..CHEST_ROOM_MAX_SIZE) return false
if (chestRoom.map { it.blockY }.minmaxDifference in CHEST_ROOM_MIN_SIZE..CHEST_ROOM_MAX_SIZE) return false
if (chestRoom.map { it.blockZ }.minmaxDifference in CHEST_ROOM_MIN_SIZE..CHEST_ROOM_MAX_SIZE) return false
igor.wojda
01/12/2022, 8:46 AMjar
file (from Android library aar
), decompiled it and I see that this Kotlin class is missing Kotlin metadata (class is not annotated with @Metadata
annotation). What could be the reason?
The issue is that library user cannot use Kotlin related constructs like named arguments, so I wonder is there a setting or compiler flag to always add Kotlin metadata? 🤔
expected result in compiled class:
@Metadata(mv = {1, 6, 0}, k = 1, xi = 48, d1 = {"\000$\n\002\030\00"}..."})
public final class MyClass {
...
}
Jason5lee
01/12/2022, 10:13 AMthis
.
class ImplByInterface2(val a: Interface2): Interface1 {...}
class MyClass : Interface1, Interace2 {
// Delegate Interface1 implementation to this
private val impl: Interface1 = ImplByInterface2(this)
}
I found it not easy to use by
. Is there any convenient way? Both language feature and IDE trick are welcomed.
Edit: makes it clearer.Jasin Colegrove
01/12/2022, 1:36 PMglenkpeterson
01/12/2022, 6:27 PMobject MemJogLib {
var testingOnlyChangeForTesting = false
val BUILD_ROOT: String = File(".").canonicalPath
private val MJL_BOOKS_DIR: String? = System.getenv("MJL_BOOKS_DIR")
@JvmStatic
fun getMjlBookDir(): String =
if (testingOnlyChangeForTesting) {
"$BUILD_ROOT/target/test-classes"
} else {
MJL_BOOKS_DIR!! // <= NullPointerException HERE
}
At the start of all tests, I make sure I call initialized:
private val initialized: Boolean by lazy {
testingOnlyChangeForTesting = true
With testingOnlyChangeForTesting = true
, the second branch of the if
statement shouldn't even be called!
When I run my tests from Maven at command line on my desktop, everything works perfectly. Using command-line Maven on the build server, I get:
java.lang.ExceptionInInitializerError
at com.goalqpc.memJogLib.TestGlobals$initialized$2.invoke(TestGlobals.kt:145)
at com.goalqpc.memJogLib.TestGlobals$initialized$2.invoke(TestGlobals.kt:144)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.goalqpc.memJogLib.TestGlobals.getInitialized(TestGlobals.kt:144)
at com.goalqpc.memJogLib.TestGlobals.testDbInMemory(TestGlobals.kt:220)
at com.goalqpc.memJogLib.servlet.MjlServletHandlerTest.before(MjlServletHandlerTest.kt:44)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:242)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:137)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.NullPointerException
at com.goalqpc.memJogLib.MemJogLib.getMjlBookDir(MemJogLib.kt:66)
at com.goalqpc.memJogLib.MemJogLib.<clinit>(MemJogLib.kt:72)
... 39 more
Itish Srivastava
01/13/2022, 1:53 AMErnest N. Wilcox Jr.
01/13/2022, 3:16 PMCoffee Guy
01/13/2022, 3:25 PMCoffee Guy
01/13/2022, 4:20 PMRyan Zidago
01/13/2022, 4:58 PMsrc/Application.kt
is mentioned but my automatically generated Application.kt
file is under src/main/kotlin/com.ryanzidago/Application.kt
🤔
Here's the content of my Application.kt
package com.ryanzidago
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import com.ryanzidago.plugins.*
import io.ktor.application.Application
import io.ktor.application.install
import com.apurebase.kgraphql.GraphQL
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
configureRouting()
}.start(wait = true)
}
fun Application.module(testing: Boolean = false) {
install(GraphQL) {
playground = true
schema {
query("hello") {
resolver { -> "World" }
}
}
}
}
Intellij tells me that there is a problem with the install
(Not enough information to infer type variable B) and playground
(Variable expected) but I cannot figure out what is wrong 🤔
When I type gradle --version
in my project root I get the following output:
ariviv
❯ gradle --version
------------------------------------------------------------
Gradle 7.3.3
------------------------------------------------------------
Build time: 2021-12-22 12:37:54 UTC
Revision: 6f556c80f945dc54b50e0be633da6c62dbe8dc71
Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 17.0.1 (Oracle Corporation 17.0.1+12-39)
OS: Linux 5.4.0-94-generic amd64
Any tips?CRamsan
01/14/2022, 5:08 PMKotlin Language Specification is still in progress and has experimental stability level, meaning no compatibility should be expected between even incremental releases, any functionality can be added, removed or changed without warning.
https://kotlinlang.org/spec/introduction.html#compatibility
What does this refer to when talking about being in experimental stability? Is this referring to the document itself or the Kotlin language?CRamsan
01/14/2022, 5:08 PMKotlin Language Specification is still in progress and has experimental stability level, meaning no compatibility should be expected between even incremental releases, any functionality can be added, removed or changed without warning.
https://kotlinlang.org/spec/introduction.html#compatibility
What does this refer to when talking about being in experimental stability? Is this referring to the document itself or the Kotlin language?Marat Akhin
01/15/2022, 9:58 AMAlexey Belkov [JB]
01/17/2022, 9:41 AM