frank
09/26/2022, 9:46 PMvidID
and idMissing
in a single variable.
data class File22(val fileName: String, val vidID: Int = - 1)
val files22 = ..... // Intance of 'File22'
val vidID = files22.map { it.vidID } // Array of Int e.g: [1,3,4,7]
val idMissing = vidID.filterIndexed { index, i -> !vidID.contains(index)} // Result: idMissing -> 2,5,6
Christian Lippka
09/26/2022, 9:51 PMimport kotlinx.serialization.Serializable
@Serializable
abstract class TagsDto( val id: String?, val name: String, val icon: String?, val color: String? )
@Serializable
class LocationTagsDto( id: String? = null, name: String, icon: String?, color: String? )
: TagsDto( id = id, name = name, icon = icon, color = color)
This fails compilation on the second @Serializable
This class is not serializable automatically because it has primary constructor parameters that are not propertiesI don’t want to have
LocationTagsDto
add any new property to TagsDto
. How can I make this work? Thanks in advance.Loney Chou
09/27/2022, 1:12 AMon
function is not inline, that lambda is considered as a closure, and because closure is self-contained, it must "capture" stuff in the outer scope, and now that it's not possible to directly modify a local variable in a closure, that local is wrapped into an object with a var member for the closure to "modify".Giuliopime
09/27/2022, 7:46 AMfun Application.configureValidator() {
install(RequestValidation) {
validate<ClientUserDto> {
val validationResult = it.validate(it)
if (validationResult is Valid)
ValidationResult.Valid
else
ValidationResult.Invalid(
validationResult.errors.map { error -> "${error.dataPath} ${error.message}" }
)
}
validate<ClientListDto> {
val validationResult = it.validate(it)
if (validationResult is Valid)
ValidationResult.Valid
else
ValidationResult.Invalid(
validationResult.errors.map { error -> "${error.dataPath} ${error.message}" }
)
}
validate<ClientListItemDto> {
val validationResult = it.validate(it)
if (validationResult is Valid)
ValidationResult.Valid
else
ValidationResult.Invalid(
validationResult.errors.map { error -> "${error.dataPath} ${error.message}" }
)
}
}
}
Tech
09/27/2022, 11:25 AMsealed class Response(val success: Boolean) {
class Success : Response(true)
class Failure : Response(false)
}
So from this I'd like to do this.
// Example
val res = Response(true)
if(res.success) {
// cast to Response.Success
} else {
// cast to Response.Failure
}
I know I can probably just use the is
clause but I think it'd be a tad bit nicer to do it like this, if it's not possible I'll just use is
.
Thanks in advance!João Gabriel Zó
09/27/2022, 6:24 PMinterface.()
mean? e.g. interface.() -> Unit
poohbar
09/27/2022, 7:11 PMdata class MyDataClass(
val package: String, // error
val hello: String
)
martmists
09/27/2022, 8:21 PMStefan Oltmann
09/28/2022, 3:10 PMprivate fun getUInt16(firstByte: Byte, secondByte: Byte): Int =
firstByte.toInt() shl 8 and 0xFF00 or (secondByte.toInt() and 0xFF)
sarvagya agarwal
09/28/2022, 4:50 PMinappropriate blocking method call
for FileInputStream
and read
functions.
suspend fun uploadFiles(files: List<File>, convertToPdf: Boolean): List<String> = coroutineScope {
return@coroutineScope files.map {
async {
uploadFile(it, convertToPdf)
}
}.awaitAll()
}
@Throws(IOException::class, FileNotFoundException::class)
suspend fun uploadFile(file: File, convertToPdf: Boolean): String = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val chunk = ByteArray(4096)
val inputStream = FileInputStream(file)
val res: Flow<FileChunk> = flow {
while(true) {
val size = inputStream.read(chunk)
if(size <= 0) break
val fileChunk = FileChunk.newBuilder().setConvertToPdf(convertToPdf)
.setChunk(ByteString.copyFrom(chunk, 0, size)).build()
emit(fileChunk)
}
}
val response = blockingStub.uploadFiles(res)
return@withContext "Some String"
}
Paul Griffith
09/28/2022, 5:20 PM@Nonnull
?Tech
09/28/2022, 10:22 PMUnit
to represent when an action has been preformed but no data is returned, so if it's anything but unit then there's data in the class.
sealed class Test<out T>(val success: Boolean) {
abstract val data: T?
class A<out T>(override val data: T?) : Test(true)
class B<out T>(override val data: T?) : Test(false)
}
fun Test<Any>.isSuccess(): Boolean {
contract {
returns(true) implies(this@isSuccess is Test.A)
// if T != Unit then data != null
}
return this is Test.A
}
Daniel Zhang
09/28/2022, 11:40 PMrunBlocking {
withTimeoutOrNull(1000) {
inputs.asSequence().repeat().asFlow()
.rateLimiter(rateLimiter)
.map { executeInput(it) }
.toList()
}
}
val rateLimiter = RateLimiter.of(
properties.name,
RateLimiterConfig
.custom()
.limitForPeriod(properties.rateLimiter.limitForPeriod)
.limitRefreshPeriod(properties.rateLimiter.limitRefreshPeriod)
.timeoutDuration(properties.rateLimiter.timeoutDuration)
.build()
)
/**
* @see <a href="<https://stackoverflow.com/questions/48007311/how-do-i-infinitely-repeat-a-sequence-in-kotlin>">Infinite Sequence</a>
*/
private fun <T> Sequence<T>.repeat() = sequence { while (true) yieldAll(this@repeat) }
Here I’m attempting to turn a list of inputs into an infinite sequence that I can then throttle with a RateLimiter + time out after a certain duration, but neither seem to be working.Hassaan
09/29/2022, 9:22 AMOlayinka Koleaje
09/29/2022, 12:27 PMKlitos Kyriacou
09/29/2022, 2:41 PMrun { ... }
. However, run
is overloaded as both an extension and non-extension function. Since the context includes an implicit "this" receiver, Kotlin favours calling the extension version. How can I force it to run the non-extension version?Daniel Felipe Henao Toro
09/29/2022, 5:49 PMroot.<String>get("codes")
but it always shows me another failure, does someone know about JPA Specification that you can help me?Meherdatta Chepuri
09/29/2022, 7:06 PMPavan Badugucse
09/29/2022, 9:53 PMrrva
09/30/2022, 10:01 AModay
09/30/2022, 10:58 AMval technologies = mapOf<String, Array<String>>("Jetpack Compose", arrayOf("Compose"))
oday
09/30/2022, 12:58 PMfor (j in 0 until pdfReader.numberOfPages) {
extractedText += PdfTextExtractor.getTextFromPage(pdfReader, j + 1)
technologies.forEach { (key, values) ->
values.map {
if (extractedText.contains(it, ignoreCase = true)
|| extractedText.contains("${it}s", ignoreCase = true)
) {
occurrences[key] = occurrences.getOrDefault(key, 0) + 1
}
}
}
}
println(occurrences)
val person = extractedText.split(" ")[0].trim { it == ',' }
results[person] = occurrences
I would like to clear occurrences
after I’m done with it so that I can load it again with new values after I insert it into results[person]
but doing that is causing it to store an empty map into `results[person]`…Tim Schraepen
09/30/2022, 1:43 PMIn Book.kt:
data class Book(val isbn: ISBN, val title: Title)
@JvmInline value class ISBN(val value: String)
@JvmInline value class Title(val value: String)
Somewhere in BookDAO.java:
new Book("123","Thundercats");
When I'm looking at the code in IntelliJ, it's simply compiling (no red squiggly lines).
When I run a mvn clean install
it also just compiles.
But when I do a build (ctrl+F9), I'm getting this:
java: Book(java.lang.String,java.lang.String) has private access in Book
When I define book as
data class Book(val isbn: String, val title: String)
compilation does succeed.
What is going on? It smells like intelliJ is using an older Kotlin version to compile or something?
In the build output I'm getting this:
...
Kotlin: kotlinc-jvm 1.7.20 (JRE 17.0.2+8)
...
javac 11.0.9 was used to compile java sources
I'd like to try to get kotlinc-jvm to use JRE 11 to compile, but don't know how.sarvagya agarwal
09/30/2022, 3:17 PMvar firstChunk = true
val fileChunks = flow {
if(firstChunk) {
val metadata = MetaData.newBuilder()
.setPath(file.path)
.setConvertToPdf(BoolValue.of(convertToPdf))
.build()
emit(FileUploadRequest.newBuilder().setMetadata(metadata).build())
firstChunk = false
} else {
while(true) {
val size = inputStream.read(chunk)
if(size <= 0) break
val fileChunk = FileChunk.newBuilder()
.setData(ByteString.copyFrom(chunk, 0, size)).build()
emit(FileUploadRequest.newBuilder().setContent(fileChunk).build())
}
}
}
Trying to create a flow of file bytes where the first byte is metadata of the file.Jasmin Fajkic
09/30/2022, 5:23 PMAbdullah Samir
09/30/2022, 11:49 PMRohan Sanap
10/02/2022, 6:54 AM@escaping
or @non-escaping
which denotes if they will be executed asynchronously or synchronously respectively. How do we differentiate between asynchronous or synchronous lambdas in Kotlin?Arimil
10/03/2022, 2:00 PMproject.getExtensions().configure(PublishingExtension.class, extension -> {
extension.getPublications().registerFactory(MavenPublication.class, new MavenPublicationFactory(
dependencyMetaDataProvider,
instantiatorFactory.decorateLenient(),
fileResolver,
project.getPluginManager(),
project.getExtensions()));
realizePublishingTasksLater(project, extension);
});
However I'm having issues with the syntax for this in Kotlin.zt
10/04/2022, 12:55 AMval values = match.groupValues.drop(1) // ["kotlin slack"]
var newResponse = filter.response!! // "hi $1"
values.forEachIndexed { index, value ->
newResponse = filter.response!!.replace("\$$index", value)
}
// newResponse = "hi kotlin slack"
Is there some cleaner way of doing this? I wanna replace $0
, $1
, $2
, etc, with its corresponding valueSlackbot
10/04/2022, 12:21 PMSlackbot
10/04/2022, 12:21 PM