myanmarking
02/03/2020, 3:52 PMMohammed Suleman
02/03/2020, 3:57 PMvpriscan
02/03/2020, 5:17 PMEyad Sibai
02/03/2020, 6:03 PMbaxter
02/03/2020, 7:50 PMPablo Schmid
02/03/2020, 9:20 PMviralshah
02/04/2020, 12:01 AMJiri Bruchanov
02/04/2020, 10:17 AMAwildrid
02/04/2020, 3:14 PMAPXEOLOG
02/04/2020, 4:29 PMgbaldeck
02/04/2020, 7:37 PMThierry
02/04/2020, 10:48 PMTravis Griggs
02/05/2020, 12:14 AMclass Slot<T> {
private var _value:T
var value:T
get() = this._value
set(newValue) {
this._value = newValue
// trigger notifications here
}
I wanted to add a variant of the value
interface for types of T
that are Comparable
that would filter out no op changes. So I added the following extension:
var <T : Comparable<T>>Slot<T>.concisely:T
get() = this.value
set(newValue) {
if (this.value != newValue) {
this.value = newValue
}
}
This works like a charm... Slot<Int>(42).concisely = 42
does just what I want. The inner value
doesn't get called.
Where this falls apart is when I have a T
that is an optional. Slot<String?>("foo").concisely = "foo"
won't even compile:
Type for parameter bound T in
var <T : Comparable<T>>Slot<T>.concisely:T
is not satisfied: inferred String? is not a subtype of Comparable<String?>
I have thrown various alternate incantations at my extension to try and accomodate optionals, but nothing seems to work. Is it even doable? What am I missing? I know that I can use ==
on optional types...Aaron Stacy
02/05/2020, 1:35 AMorg.jetbrains.kotlin.types
or <http://org.jetbrains.kotlin.ir|org.jetbrains.kotlin.ir>
?steenooo
02/05/2020, 8:36 AMTimur Atakishiev
02/05/2020, 9:20 AMKroppeb
02/05/2020, 10:39 AMKroppeb
02/05/2020, 11:05 AMGurupad Mamadapur [FH]
02/05/2020, 12:19 PMclass BaseResponse<T> {
val data: T? = null
var someMessage: String? = null
}
class User {
val name: String? = null
}
fun main() {
val name = getUser<User>().data?.name
println(name)
}
inline fun <reified R> getUser(): BaseResponse<R> {
return if (testMessage == null) {
BaseResponse<R>().apply { someMessage = "Some error" }
} else {
Gson().fromJson(
testMessage,
object : TypeToken<BaseResponse<R>>() {}.type
)
}
}
val testMessage: String? = """{"data": {"name":"Adam"},"someMessage":""}"""
Error -
Exception in thread "main" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class User (com.google.gson.internal.LinkedTreeMap and User are in unnamed module of loader 'app')
at MainKt.main(main.kt:54)
at MainKt.main(main.kt)
If I send BaseResponse<User> as R instead of just User and return R, it works fine. But I want to return BaseReponse<R>.Kris Wong
02/05/2020, 3:10 PMvar
reference from the adjacent scope in one of its methods, does it not see changes to the original reference (i.e., it captures its own reference to the value when the object is created)?kyleg
02/05/2020, 4:03 PMclass MyFragment : ... {
@Inject lateinit var mViewModelFactory: ViewModelProvider.Factory
private val mViewModel by lazy {
ViewModelProviders
.of(requireActivity(), mViewModelFactory)
.get(MyViewModel::class.java) // MyViewModel extends ViewModel
}
}
Any idea how I might parametrize this into a base class to ideally get rid of ALL this boilerplate? I have
abstract class BaseFragment<VM: ViewModel>: ... {
abstract var mViewModelFactory: ViewModelProvider.Factory
private val mViewModel by lazy {
ViewModelProviders
.of(requireActivity(), mViewModelFactory)
.get(?????)
}
}
This wuold at least enable
class MyFragment<MyViewModel> {
@Inject lateinit var mViewModelFactory: ViewModelProvider.Factory
}
But I’m stuck here. I thought about making a lookup table to map fragment classes to viewmodel classes, but would like to avoid that if possible.
Thoughts? I doubt it can be done, but thought I’d ask here.Mikołaj Karwowski
02/05/2020, 4:05 PMkenkyee
02/05/2020, 4:57 PMShan
02/05/2020, 9:20 PMdata class Foo()
in my network library, but I would like to add an annotation to it in my Android app
module for use with my database.Travis Griggs
02/05/2020, 10:45 PMif let foo = someOptionalExpression, let bar = optionalExpression(foo) {
// work with foo AND bar
}
else {
// deal with nil/null case
}
Czar
02/06/2020, 11:27 AMsuspend inline fun <reified T : Event> Client.addHandler(noinline action: suspend (T) -> Unit)
I have a handler interface
interface EventHandler<T : Event> {
suspend fun handle(event: T)
}
And I have a list of handlers which is injected into my application: val handlers: List<EventHandler<*>>
Each handler is defined as MyHandler : EventHandler<MyEvent>
I need to somehow add these handlers to the client: handlers.forEach { client.addHandler(it::handle) }
I'm getting
Cannot use 'Nothing' as reified type parameterat the
.addHandler
What can I do to work around it?Sam Garfinkel
02/06/2020, 4:50 PMgbaldeck
02/06/2020, 6:32 PMlatinit val
in Kotlin?Jason
02/07/2020, 4:36 AMlocal function
in Kotlin?
( As far as I know, it is used for reuse common code inside a function )zjuhasz
02/07/2020, 5:10 AMzjuhasz
02/07/2020, 5:10 AMserebit
02/07/2020, 5:26 AMDslMarker
annotationzjuhasz
02/07/2020, 5:28 AMKroppeb
02/10/2020, 11:21 PM