basher
06/03/2020, 11:38 PMAndrew
06/04/2020, 12:15 AMelect
06/04/2020, 11:37 AMtasks.withType(JmhTask::class.java) { }
where tasks
is a TaskContainer
type, which extends interface DomainObjectCollection<T>
DomainObjectCollection<T>::withType
is defined as <S extends T> DomainObjectCollection<S> withType(Class<S> type, Action<? super S> configureAction)
I'd like to type instead tasks<JmhTask> { }
, so I wrote:
inline operator fun <reified S> TaskContainer.invoke(configureAction: Action<in S>?) = withType(S::class.java, configureAction)
but withType
is marked red:
None of the following functions can be called with the arguments supplied.
withType(Class<TypeVariable(S)!>, Action<in TypeVariable(S)!>) where S = TypeVariable(S) for fun <S : Task!> withType(type: Class<S!>, configureAction: Action<in S!>): DomainObjectCollection<S!> defined in org.gradle.api.tasks.TaskContainer
darkmoon_uk
06/04/2020, 11:59 AMMike
06/04/2020, 12:00 PMuser
06/04/2020, 3:30 PMAntoine Gagnon
06/04/2020, 4:10 PMval originalNumbers = listOf(1,3,5,6,8,235,764)
val newNumbers = listOf(1,4,6,235,764,23)
val toRemove = originalNumbers.filter { it !in newNumbers } // [3, 5, 8]
This work perfectly fine, but just for my own peace of mind I feel like there’s a collections functions that would do exactly that?Sam Garfinkel
06/04/2020, 4:11 PMwilliam
06/04/2020, 6:04 PMpublic class FooService {
public void addFoo(Foo foo) {
// woop
}
public void removeFoo(Foo foo) {
// womp
}
}
interface Foo {
void callback();
}
and then in the kotlin code I have:
class FooKotlin {
fun buzz() {
val fooService = FooService()
fooService.addFoo {
// do some callback stuff
fooService.removeFoo(???)
}
}
}
I'm not sure what to replace the ???
with in order to remove the callback listener after it has executed onceDraget
06/04/2020, 8:34 PMKenneth Endfinger
06/04/2020, 10:03 PMNurBahnhof
06/05/2020, 12:19 AMandyg
06/05/2020, 6:17 AMwhen
statement dynamically from a map, or two arrays/lists? something like
val teamMap : Map<String, String> = mapOf("Yankees" to "baseball", "Cowboys" to "football", "Lakers" to "basketball")
val sport : String = when {
teamMap.map { e -> mystring.contains(e.key) -> e.value }
}
also a value for else
would need to be supplied, not sure the best wayBrian Dilley
06/05/2020, 7:16 AMGilles Barbier
06/05/2020, 11:39 AMopen class MyInt(open var int: Int = 0) : {
operator fun plus(increment: Int): MyInt = MyInt(int + increment)
}
I can do
MyInt(4) + 3
It works. But now, I would like to extends MyInt
class OtherInt(override var int: Int = 0) : MyInt(int)
I can do OtherInt(4) + 3
but the output is a MyInt.
Is there a way for OtherInt
to benefit from operator overloading without reimplementing it in it?rrva
06/05/2020, 11:48 AMwhen
over a type system, like
when {
foo is A ->
foo is B ->
}
why can the compiler not figure out compile-time if the when is exhaustive without an else branch?Daniel Svensson
06/05/2020, 11:58 AMfoo.let(Bar::method)
vs foo.let { Bar.method(it) }
in terms of compiler speed, thinking that the function reference opens up to fewer possibilities compared to a block, and it could have a slight, but measurable compilation speedup if used a lot. Generating a 1000 long file with identical .let { .. }
and another file with .let(func::ref)
resulted in .let { ... }
being consistently slightly faster with 7.125 seconds vs 7.882 seconds. One hypothesis is that the function reference version is transformed to the block version during compilation, perhaps someone here knows more of the compiler internals to pitch in on that. As expected identical byte code is generated in both cases. When I then tried with Kotlin 1.4 M2, it turned out that the .let { ... }
version had become slower, and has now consistently the exact same compilation speed as the function reference. Which is the reason to why I signed up to this slack 🙂 Was hoping for the opposite as 1.4 is touted to improve compilation speed (but it's not like it's life or death which style is preferred).Ryan
06/05/2020, 1:35 PMpoohbar
06/05/2020, 4:43 PMClass
from a KClass
? For example I have
KClassImpl "class kotlin.String"
and I want to get Java’s String.class
from it
I tried:
JvmClassMappingKt.getJavaClass(klass)
but it returns class kotlin.reflect.jvm.internal.KClassImpl
😕Sam Garfinkel
06/05/2020, 5:49 PMT: Any?
differently from T: Any
without conflicting overloads? The following conflict:
inline fun <reified T: Any> foo(): Bar<T> {}
inline fun <reified T: Any, S: T?> foo(): Bar<S> {}
Which makes sense since it’s ambiguous for the call when T is non-null.Hullaballoonatic
06/05/2020, 10:34 PMcompanion object
the kookiest kotlin-specific syntax for a typical OOP language feature? y/y?juan torres
06/06/2020, 1:53 AMJavier
06/06/2020, 2:16 PMe: java.lang.IllegalStateException: Backend Internal error: Exception during file facade code generation
Animesh Sahu
06/07/2020, 5:46 AM{Comparable<{ Double & Int }> & Number}
https://stackoverflow.com/questions/62241262/understanding-kotlin-type-system-what-is-meant-by-comparable-double-intDavide Giuseppe Farella
06/07/2020, 6:46 AMclass MyClass<T>
MyClass<String?>().myExtension() // -> OK
MyClass<String >().myExtension() // -> Does not compile?
E.Kisaragi
06/07/2020, 3:58 PMFoo!!
mean??Hamza
06/08/2020, 6:22 AMsuspend
and an operator
fun?Patrick Lannigan
06/08/2020, 10:58 AMfun thing(a: ((String) -> String)?) {
if (a == null) {
println("null")
} else {
println(a("hello"))
}
}
class Thing(private val a: ((String) -> String)?) {
fun thing() {
if (a == null) {
println("null")
} else {
// Reference has a nullable type '((String) -> String)?', use explicit '?.invoke()' to make a function-like call instead
println(a("hello"))
}
}
}
Reproducing the issue using the playgroundMichael de Kaste
06/08/2020, 12:40 PMif(yourValue == SOME_VALUE)
where SOME_VALUE is a const val defined in an interface. Can I show the value of this value passively like inferred types are passively shown in the code?jeggy
06/08/2020, 4:26 PMimplementation(kotlin("stdlib"))
and implementation(kotlin("stdlib-jdk8"))
?