rrva
07/03/2020, 5:58 AMIfvwm
07/03/2020, 9:41 AMandylamax
07/03/2020, 10:32 AMfun <T> Foo()
and fun <T:Any> Foo()
?
So far I have just been using IntelliSense suggestions. I wan't to understand more. Any deep explanation?andylamax
07/03/2020, 11:45 AM@Serializable
sealed class Result<out T> {
@Serializable
class Success<out T>(val data: T) : Result<T>()
@Serializable
class Failure<out T>(val msg: String) : Result<T>()
companion object {
private fun <T> resultContext(serializer: KSerializer<T>) = SerializersModule {
polymorphic<Result<T>> {
subclass(Success.serializer(serializer))
subclass(Failure.serializer(serializer))
}
}
fun <T> stringify(serializer: KSerializer<T>, res: Result<T>): String {
return Json(context = resultContext(serializer)).stringify(
Result.serializer(serializer),
res
)
}
fun <T> parse(serializer: KSerializer<T>, json: String): Result<T> {
return Json(context = resultContext(serializer)).parse(
Result.serializer(serializer),
json
)
}
}
}}
So that If I have a class
@Serializable
data class Person(val name:String)
I can call
Result.stringify(Result.Serializer(Person.serializer()),Result.Success(Person("Andy))) // This says class Person is not registered for polymorphic serialization
I have little knowledge in Polymorphic Serialization.
Also, I need to be able to serialize a Result.Success<Person?>(null)
. Currently I don't know how to do so. Help?Nishoobansal
07/03/2020, 3:56 PMLost Illusion
07/03/2020, 8:40 PMAhmed Mourad
07/03/2020, 10:08 PMVictor Harlan Lacson
07/04/2020, 1:59 AMThorkild
07/05/2020, 10:33 AMDmitry Kandalov
07/06/2020, 11:15 AMnfrankel
07/06/2020, 1:14 PMwhile (shorteneds.putIfAbsent(url, this) != null) {}
of course, intellij complains that the block is empty
any idea how to write a better version without the complaint?
i’m probably missing something from stdlib...Nishoobansal
07/06/2020, 4:57 PMFleshgrinder
07/06/2020, 5:09 PMDariusz Kuc
07/06/2020, 5:53 PMfun <T : AbstractStub<*>> getClient(stubFactory: (Channel) -> T): T {
val client = stubFactory.invoke(configuredChannel)
return if (deadlineMs != null) {
// returns AbstractStub<S>
client.withDeadlineAfter(deadlineMs, TimeUnit.MILLISECONDS) as T
} else {
client
}
}
was playing around with inlining the T
but cannot get it to work
---
doing the chaining call from from the caller works as expected
val client: HelloGrpc.HelloBlockingStub = getClient { HelloGrpc.newBlockingStub(it) }.withDeadlineAfter(1000, TimeUnit.MILLISECONDS)
itnoles
07/06/2020, 9:00 PMgroostav
07/06/2020, 10:59 PMLinkedHashMap
is a nice type in that its got the time complexities of a hash map and it preserves insertion order... but its implementation really is pretty gnarly. Does anybody have numbers on how its implementation impacts performance? Is there a more performant (and/or simpler and/or compact) implementation of an insertion-order-preserving-Map if I'm willing to give up some of the more esoteric intricacies of LinkedHashMap
(eg: degradation to a b-tree if hashCode == 0
, dynamic-checking for Comparable
implementations, etc)? Immutability (even via copy-on-write) is also fine. I have maps that have an upper-bound size of 200, and I'm looking to get to lookups on the order of millions per second.Orhan Tozan
07/07/2020, 10:43 AMfun interface CreateTodoUseCase { fun run() }
over interface CreateTodoUseCase { operator fun invoke() }
? I see the latter being used alot for writing UseCases (myself included. Should I switch to functional interfaces instead?Jakekudur
07/07/2020, 1:47 PMwilliam
07/07/2020, 2:44 PM>
, <=
, etc as an argument to a function?Sam Garfinkel
07/07/2020, 3:11 PMxii
07/07/2020, 4:24 PMdave08
07/08/2020, 10:19 AMinterface Something {
fun foo(bar: Int): List<Int>
}
class SomethingImpl : Something {
//...
}
val ref = Something::foo
and I want to invoke it, do I do this:
val instance = SomethingImpl()
ref.invoke(instance, 5)
?Rob Murdock
07/08/2020, 2:04 PMasad.awadia
07/08/2020, 3:14 PMJim
07/08/2020, 4:36 PMxii
07/08/2020, 5:53 PMthana
07/09/2020, 9:18 AMComparable
is violated but io cannot spot the violationGilles Barbier
07/09/2020, 3:03 PMbbaldino
07/09/2020, 5:59 PMclass B {
fun Int.answer(): Int { return 42 }
}
and somewhere else be able to do something like:
fun main() {
val myB = B()
val num: Int = 10
num.apply {
myB.answer()
}
}
My quick test tells me no, but is it imposible/nonsensical or am I just invoking it wrong? I want to be able to implement a function on some type A using some state from an instance of type B.bbaldino
07/09/2020, 11:25 PMKClass<T>
and returns a function which takes a String
and returns an instance `T`:
fun <T : Any> getterFor(valueType: KClass<T>): (String) -> T
One issue with this is that, due to type erasure, it doesn't work for things like List<Int>
. I know `typeOf`/`KType` can help with this, but is there a typesafe way I can refer to a KType
argument's type in the return type position? I.e. before I have T
which matches between the argument (KClass<T>
) and the return type T...is there a way to do that with KType?
fun getterFor(type: KType): (String) -> ???
bbaldino
07/09/2020, 11:25 PMKClass<T>
and returns a function which takes a String
and returns an instance `T`:
fun <T : Any> getterFor(valueType: KClass<T>): (String) -> T
One issue with this is that, due to type erasure, it doesn't work for things like List<Int>
. I know `typeOf`/`KType` can help with this, but is there a typesafe way I can refer to a KType
argument's type in the return type position? I.e. before I have T
which matches between the argument (KClass<T>
) and the return type T...is there a way to do that with KType?
fun getterFor(type: KType): (String) -> ???
fun <T : Any> getterFor(type: KType): (String) -> T
and then cast to T
, but wondering if there's a way to 'enforce' a relationship between the KType and the return value's typeZach Klippenstein (he/him) [MOD]
07/09/2020, 11:42 PMinline fun <reified T : Any> getterFor(): (String) -> T {
@Suppress("UNCHECKED_CAST")
return getterFor(typeOf<T>()) as (String) -> T
}
@PublishedApi internal fun getterFor(type: KType): (String) -> Any {
…
}
bbaldino
07/09/2020, 11:43 PM