hhariri
01/27/2021, 12:52 PMBig Chungus
01/27/2021, 5:29 PMrajesh
01/28/2021, 7:45 AMuser
01/28/2021, 8:00 AMLeon K
01/28/2021, 11:13 AMstefandekanski
01/28/2021, 12:50 PMfun <T, M : MutableCollection<T>> Collection<T>.partitionTo(left: M, right: M, predicate: (T) -> Boolean): Pair<Collection<T>, Collection<T>> {
forEach {
if (predicate(it)) left += it else right += it
}
return Pair(left, right)
}
so as this:
fun <T, C : Collection<T>, M : MutableCollection<T>> C.partitionTo(left: M, right: M, predicate: (T) -> Boolean): Pair<Collection<T>, Collection<T>> {
forEach {
if (predicate(it)) left += it else right += it
}
return Pair(left, right)
}
This doesn’t compile… why?
fun <T, C : Collection<T>, M : MutableCollection<T>> C.partitionTo(left: M, right: M, predicate: (T) -> Boolean): Pair<C, C> {
forEach {
if (predicate(it)) left += it else right += it
}
return Pair(left, right)
}
when you cast it, then i works then but it’s unsafe cast..
fun <T, C : Collection<T>, M : MutableCollection<T>> C.partitionTo(left: M, right: M, predicate: (T) -> Boolean): Pair<C, C> {
forEach {
if (predicate(it)) left += it else right += it
}
return Pair(left as C, right as C)
}
Why can’t typesystem figure out that M is covariant towards C ?Youssef Shoaib [MOD]
01/28/2021, 3:42 PMifEmpty
extension function on String is defined like this in the Kotlin stdlib:
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
if (isEmpty()) defaultValue() else this
However, if you try to define exactly the same function in your own code you get this error: Type parameter cannot have any other bounds if it's bounded by another type parameter
So how is the standard library circumventing this error? I even checked and there aren't any @Suppress
annotations in the file at all regarding this, so how in the world did they stop that error?Kunal Dabir
01/28/2021, 5:40 PMManuel Dupont
01/29/2021, 7:50 AMalexandrepiveteau
01/29/2021, 1:36 PM@DslMarker
annotation on a suspend
scope receiver ?
I’m getting an java.lang.IllegalStateException: Backend Internal error: Exception during IR lowering
exception when I write the following code, but don’t face any issue when I remove the suspend
modifier :
@DslMarker
annotation class MyDslMarker
@MyDslMarker
interface MyDsl
fun something(
scope: suspend MyDsl.() -> Unit,
) {
}
fun main() {
something { }
}
Karlo Lozovina
01/29/2021, 4:52 PMkotlinx-benchmark
with .kts Gradle scripts? example at https://github.com/Kotlin/kotlinx-benchmark/tree/master/examples is a complex one, and trying to simplify I'm getting nowhere...Anarakul
01/29/2021, 4:59 PMGayan Perera
01/29/2021, 7:31 PMLogan Knight
01/29/2021, 8:39 PMFlorian
01/30/2021, 3:56 PMinline fun <ResultType, RequestType> networkBoundResource(
crossinline query: () -> Flow<ResultType>,
crossinline fetch: suspend () -> RequestType,
crossinline saveFetchResult: suspend (RequestType) -> Unit,
crossinline onFetchSuccess: () -> Unit = { },
crossinline onFetchFailed: (Throwable) -> Unit = { },
crossinline shouldFetch: (ResultType) -> Boolean = { true }
) = flow {
val data = query().first()
val flow = if (shouldFetch(data)) {
emit(Resource.Loading(data))
try {
saveFetchResult(fetch())
onFetchSuccess()
query().map { Resource.Success(it) }
} catch (t: Throwable) {
onFetchFailed(t)
query().map { Resource.Error(t, it) }
}
} else {
query().map { Resource.Success(it) }
}
emitAll(flow)
}
Nikita Voloshin
01/30/2021, 4:12 PMursus
01/31/2021, 11:18 AM@Provides
@JvmStatic
fun widgetUpdater() ..
Can I turn it somehow to this? where @ProvidesAndJvmStatic
would reference then the daggers @Provide
and kotlins @JvmStatic
inside?
@ProvidesAndJvmStatic
fun widgetUpdater() ..
oday
01/31/2021, 12:42 PMStragaSevera
01/31/2021, 1:42 PMstart()
method, but does not happen if I remove the class and copy contents of the start()
method to the main()
function. It seems like there is some caching issue...stephanmg
01/31/2021, 3:29 PMRob Elliot
01/31/2021, 7:31 PMdiniamo
01/31/2021, 10:25 PMjavaru
02/01/2021, 2:12 PMthis
being used?
In my IntelliJ Plugin have this:
fun Project.foo {
...
service<SomeApplicationService>().bar()
}
The com.intellij.openapi.components.service.kt
file has two service
functions:
inline fun <reified T : Any> service(): T = ApplicationManager.getApplication().getService(T::class.java)
inline fun <reified T : Any> Project.service(): T = getService(T::class.java)
Because I'm inside a Project
extension function, it is implicitly calling this.service<SomeApplicationService>().work()
and thus the Project.service()
extension function rather than service()
. I was able to workaround by creating a separate applicationService()
convenience function. But I have to imagine this is not an unheard of situation and was wondering if there is some syntax to say not to use the implicit this
. It's a top-level function, to I'm not aware of any this@
syntax for this case.Florian
02/01/2021, 3:59 PMval sortedArticles = cachedArticles.sortedBy { article ->
article.updatedAt
}
val oldestTimestamp = sortedArticles.firstOrNull()?.updatedAt
Karlo Lozovina
02/01/2021, 4:42 PMfoobar
so that I can pass ::class of either subclasses? https://pl.kotl.in/Y5NcoKR3KMarko Mitic
02/02/2021, 5:12 AMMjahangiry75
02/02/2021, 7:40 AMStateFlow
doesn't emit new changes of a MutableList
?
this is my code:
_selectedLanguages.value = _selectedLanguages.value.apply {
if (changedLanguage.isSelected)
add(changedLanguage)
else
remove(changedLanguage)
}
Ben Butterworth
02/02/2021, 10:36 AMrajesh
02/02/2021, 4:00 PM@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = CustomerTypeSubSetValidator.class)
public @interface CustomerTypeSubset {
CustomerType[] anyOf();
String *message*() default "must be any of {anyOf}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
AB
02/02/2021, 9:44 PMclass Mapper
{
// can I have a one generic method, instead of two ??
like: fun <T> MapKeyword (<U> keyword){ return T().SetId(keyword.Id).Build() }
fun MapKeywordOne(KeywordOne keywordOne) : keyWordOneBuilder
{ return keyWordOneBuilder().SetId(keywordOne.Id).Build() }
fun MapKeywordTwo(KeywordTwo keywordTwo) : keyWordTwoBuilder
{ return keyWordTwoBuilder().SetId(KeywordTwo.Id).Build() }
}
final class keyWordOneBuilder // this one is from another package and I cannot modify
{
setId(){ // implementation }
build(){ // implementation }
}
final class keyWordTwoBuilder // this one is from another package and I cannot modify
{
setId(){ // implementation }
build(){ // implementation }
}
final class KeywordOne // this one is also sealed from a different package
{
public int Id;
}
final class KeywordTwo // this one is also sealed from a different package
{
public int Id;
}
AB
02/02/2021, 9:44 PMclass Mapper
{
// can I have a one generic method, instead of two ??
like: fun <T> MapKeyword (<U> keyword){ return T().SetId(keyword.Id).Build() }
fun MapKeywordOne(KeywordOne keywordOne) : keyWordOneBuilder
{ return keyWordOneBuilder().SetId(keywordOne.Id).Build() }
fun MapKeywordTwo(KeywordTwo keywordTwo) : keyWordTwoBuilder
{ return keyWordTwoBuilder().SetId(KeywordTwo.Id).Build() }
}
final class keyWordOneBuilder // this one is from another package and I cannot modify
{
setId(){ // implementation }
build(){ // implementation }
}
final class keyWordTwoBuilder // this one is from another package and I cannot modify
{
setId(){ // implementation }
build(){ // implementation }
}
final class KeywordOne // this one is also sealed from a different package
{
public int Id;
}
final class KeywordTwo // this one is also sealed from a different package
{
public int Id;
}
Tyler Hodgkins
02/02/2021, 10:30 PMreified
and a bit of reflection magic to create an instance, but that would rely on a no-arg constructor.AB
02/02/2021, 11:00 PMashmelev
02/03/2021, 12:28 AMTyler Hodgkins
02/03/2021, 12:44 AM