dave08
05/04/2021, 1:42 PMAngeles Bilbao
05/04/2021, 1:44 PMAndrea Giuliano
05/04/2021, 4:48 PMfun myFancyMethod(function: () -> O)
so that a caller can use it this way:
myfancyMethod{
context.giveMeSomething() //context or it.context is available since it's set somehow from myFancyMethod
}
Is that feasible?Karlo Lozovina
05/04/2021, 4:59 PMSomeClass::class.sealedSubclasses
method? Sometimes I need to gradle clean
my project for it to return correct results. Happened with 1.4 and now with 1.5 RC versionsDany
05/04/2021, 9:40 PMclass SeonApi {
companion object {
fun getEmailScore(emailInput: String): String {
return "hello"
var request: HttpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("<https://api.seon.io/SeonRestService/email-api/v2.0/>".plus(emailInput)))
.setHeader("X-API-KEY", @Value("\${seon_api_key}")) //***************complained: "Annotation class cannot be instantiated"
.build()
var response: HttpResponse<String> = HttpClient.newHttpClient().send(request, BodyHandlers.ofString())
return response.body()
}
}
}
This piece of code failed to compile and the debug. Had been spending 30 minutes debuging + googling without any success, anyone has a suggestion?Alessandro Tagliapietra
05/04/2021, 10:45 PM.filter { _key, value ->
value.getTimestamp().isBefore(Instant.now().plusSeconds(600))
}
key
is a string but I don't care what type it is
value
can be multiple types but all implement the getTimestamp method that returns an instant, so I've tried tried to create a file like this:
interface DataWIthTimestamp {
fun getTimestamp(): Instant
}
val filterFutureData: (key: Any, value: DataWIthTimestamp) -> Boolean = { _, value ->
value.getTimestamp().isBefore(Instant.now().plusSeconds(600))
}
which doesn't work as it says
Type mismatch.
Required: Predicate<in String!, in Metric!>!
Found: (Any, DataWIthTimestamp) → Boolean
I know my implementation might not make sense 🙂itnoles
05/04/2021, 10:49 PMJ6ey
05/05/2021, 2:55 AMPatrick Ramsey
05/05/2021, 3:49 AMgattytto
05/05/2021, 5:31 AMjean
05/05/2021, 5:44 AMinterface Executor<T, U> {
fun isExecutable(): (T) -> Boolean
fun execute(): suspend (U) -> Unit
}
class MyClass<T, U> {
val executors = mutableMapOf<String, Executor<T, U>>()
fun register(eventName: String, executor: Executor<T, U>) {
executors[eventName] = executor
}
inline fun <reified V : U> register(executor: Executor<T, V>) {
val eventName = V::class.toString()
register(eventName, executor)
}
}
I get the following error from the IDE Type mismatch. Required: Executor<T, U>, Found: Executor<T, V>
U
is a sealed class and V
a subtype of that class. I did read the page about generic and “PECS” in the doc, but I still haven’t figured it out. Anyone knows what I need to change?huehnerlady
05/05/2021, 6:31 AMhho
05/05/2021, 9:58 AMAndrew Ebling
05/05/2021, 11:35 AMfun myFunction()
. The body of both implementations is different, but the last line is the same, calling a function in the abstract superclass.
The common last line must be executed last. What is the most appropriate way to use Kotlin language features to refactor out the duplication?Cicero
05/05/2021, 2:17 PMPablo
05/05/2021, 3:49 PMclass ProductModel {
val productListModel: List<ProductListModel>? = null,
}
And I have the mapper like this
fun map(input : ProductModel) : List<Product>{
return input.productListModel?.map{Product(....)}.orEmpty()
}
Is there another elegant way? If the list is null or empty I should return an empty List of Product and if not, the list mappedPeter Ertl
05/05/2021, 4:35 PMPeter
05/05/2021, 4:36 PMExpression 'seconds' cannot be invoked as a function. The function 'invoke()' is not found
… literally looking at the seconds function in the Duration companion object 😳Peter Ertl
05/05/2021, 4:45 PMif (klass.isValueClass) {
// TODO get type of embedded value for further processing
}
update - I think I found it:
if (klass.isValue) {
val ctor = klass.primaryConstructor ?: error("value class has no primary constructor")
val valueType = ctor.parameters.firstOrNull()?.type ?: error("value class has no single parameter in constructor")
// use it
}
If somebody knows a shorter way I would appreciatePablo
05/05/2021, 5:23 PMfun createDummyProduct(number: Int = 1) = Product(id = "id$number", name = "name$number", title = "title$number")
Paul Griffith
05/05/2021, 8:32 PMShailan Patel
05/05/2021, 8:41 PMPhilip Dukhov
05/06/2021, 6:10 AMSealed.Subclass
, but is there a way to define new subclass in an other file but still be able to have it inside Sealed
"namespace"?
class Sealed.NewSubclass: Sealed()
is not possible, but maybe there's an alternative?
The reason why I need it is because I wanna generate some of sealed subclasses, and it's very convenient to have all subclasses under single namespacehuehnerlady
05/06/2021, 8:34 AMEquilat
05/06/2021, 9:02 AMalllex
05/06/2021, 9:10 AM@SinceKotlin("1.3")
@kotlin.internal.InlineOnly
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
if (isEmpty()) defaultValue() else this
You can call it and it works great. However, if I simply copy this function into my code (with intention to later have a slightly modified version), it simply does not compile:
public inline fun <C, R> C.ifEmpty2(defaultValue: () -> R): R where C : CharSequence, C : R =
if (isEmpty()) defaultValue() else this
Produces error highlighting `CharSequence`:
Type parameter cannot have any other bounds if it's bounded by another type parameter
You can find the example here: https://pl.kotl.in/oGzbGuXah
Why is this the case? Why can’t I use the same features used in stdlib?Vivek Modi
05/06/2021, 10:51 AMSubodh Nijsure
05/06/2021, 12:19 PMprivate fun ImageFile.toJavaAttachment(): JavaAttachment {
return JavaAttachment().apply {
/* Here imageName is correctly referenced */
fileName = imageName
/* compiler should have warned that this is a self-assignment? */
updateTime = updateTime
}
}
package org.kotllinlang.play
import JavaAttachment
data class ImageFile (
val imageName: String,
val imageType: Int,
val updateTime: String
)
data class KotlinAttachment (
var fileName: String = "default-kotlin-file-name",
var updateTime: String = "default-kotlin-time"
)
fun main() {
val image = ImageFile(
imageName = "file.jpg",
imageType = 1,
updateTime = "this-is-updated-time-stamp")
val javaAttachment = image.toJavaAttachment()
val kotlinAttachment = image.toKotlinAttachment()
println("Java attachment $javaAttachment")
println("Kotlin attachment $kotlinAttachment")
}
private fun ImageFile.toKotlinAttachment(): KotlinAttachment {
return KotlinAttachment().apply {
fileName = imageName
/* compiler correctly generates a self-assignment warning */
updateTime = updateTime
}
}
private fun ImageFile.toJavaAttachment(): JavaAttachment {
return JavaAttachment().apply {
/* Here imageName is correctly referenced */
fileName = imageName
/* compiler should have warned that this is a self-assignment? */
updateTime = updateTime
}
}
package org.kotllinlang.play;
public class JavaAttachment {
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
private String fileName;
private String updateTime;
public JavaAttachment() {
updateTime = "default-java-time";
fileName = "default-java-file-name";
}
@Override
public String toString() {
return "JavaAttachment{" +
"fileName='" + fileName + '\'' +
", updateTime='" + updateTime + '\'' +
'}';
}
}
CFIG
05/06/2021, 2:58 PMorg.jetbrains.kotlin.jvm
to 1.5.0
with subproject dependency?
An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.jvm', version: '1.5.0']
> Failed to apply plugin 'org.jetbrains.kotlin.jvm'.
> Gradle#projectsEvaluated(Action) on build 'boot' cannot be executed in the current context.
I am using build.gradle.kts, It works fine with kotlin 1.4.31, but not 1.5.0.Matěj Bystřický
05/06/2021, 3:49 PMid = action.id ?: state.activeId ?: -1