generics

How to implement the same interface multiple times, but with different generics? [duplicate]

感情迁移 提交于 2020-07-15 00:30:19
问题 This question already has answers here : How to make a Java class that implements one interface with two generic types? (9 answers) Closed 6 years ago . I have the following interface, which I want to implement multiple times in my classes: public interface EventListener<T extends Event> { public void onEvent(T event); } Now, I want to be able to implement this interface in the following way: class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent> { @Override public void

How to implement the same interface multiple times, but with different generics? [duplicate]

妖精的绣舞 提交于 2020-07-15 00:30:14
问题 This question already has answers here : How to make a Java class that implements one interface with two generic types? (9 answers) Closed 6 years ago . I have the following interface, which I want to implement multiple times in my classes: public interface EventListener<T extends Event> { public void onEvent(T event); } Now, I want to be able to implement this interface in the following way: class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent> { @Override public void

How to implement the same interface multiple times, but with different generics? [duplicate]

北战南征 提交于 2020-07-15 00:26:03
问题 This question already has answers here : How to make a Java class that implements one interface with two generic types? (9 answers) Closed 6 years ago . I have the following interface, which I want to implement multiple times in my classes: public interface EventListener<T extends Event> { public void onEvent(T event); } Now, I want to be able to implement this interface in the following way: class Foo implements EventListener<LoginEvent>, EventListener<LogoutEvent> { @Override public void

Cannot make a generic item in Kotlin

浪尽此生 提交于 2020-07-10 10:26:45
问题 I want to make the following line of code generic: val newItem: Item = documentChange.document.toObject<Item>(Item::class.java) Works fine. However, when I try to make it generic: val addedItem: T = documentChange.document.toObject<T>(T::class.java) Android Studio is complaining: Cannot use 'T' as reified type parameter. Use a class instead. Screenshot How can I make it generic? 回答1: You need to make a function inline and add reified keyword to the generic parameter: inline fun <reified T>

How to use 'I' as reified type parameter in a LiveData<I> class?

旧城冷巷雨未停 提交于 2020-07-10 10:26:38
问题 I'm trying to use generics when subclassing a LiveData<I> class . According to this answer, I have tried this: class ItemLiveData<I>(): LiveData<I>() { override fun onEvent(querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException?) { if (e != null) return for (documentChange in querySnapshot!!.documentChanges) { when (documentChange.type) { DocumentChange.Type.ADDED -> setValue(getItem(documentChange)) //Add to LiveData } } private inline fun <reified I> getItem(doc: DocumentChange) = doc

How do I share a struct containing a phantom pointer among threads?

一曲冷凌霜 提交于 2020-07-08 12:27:25
问题 I have a structure that needs to be generic over a type, yet the type is not actually contained in the structure: it's used in methods of this structure, not in the structure itself. And so, the structure includes a PhantomData member: pub struct Map<T> { filename: String, phantom: PhantomData<*const T>, } The phantom member is defined as a pointer because the structure does not actually own data of type T . This is per advice in the documentation of std::marker::PhantomData: Adding a field

SwiftUI TabView with List not refreshing after objected deleted from / added to Core Data

谁都会走 提交于 2020-07-08 00:34:03
问题 Description: When an object in a list (created from a fetchrequest) is deleted from a context, and the context is saved, the list does not properly update. Error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value (Thrown on line 5 below) struct DetailView: View { @ObservedObject var event: Event var body: some View { Text("\(event.timestamp!, formatter: dateFormatter)") .navigationBarTitle(Text("Detail")) } } Steps to reproduce: Create a new Master Detail App

TypeScript how to create a generic type alias for a generic function?

无人久伴 提交于 2020-07-07 12:27:17
问题 This has probably been answered somewhere , but I haven't found it in looking at a half-dozen questions and TypeScript issues. Given a typed generic function, I want to create a generic alias for that function, but it seems I can't. In other words, this doesn't work: // foo.tsx function foo<T>(arg: T): T { return arg } type FooT = typeof foo // works, but alias isn't generic: <T>(arg: T) => T type FooParametersT = Parameters<typeof foo> // sure: [unknown] type FooReturnT = ReturnType<typeof

How to make Parameters of VB.NET function as Generic type?

≯℡__Kan透↙ 提交于 2020-07-06 11:53:17
问题 I have a VB.NET function as below, the parameter 'x' that is passed to the function is of Type 'Single'. However, I want to write the function so that it can accept any numeric type such as 'Single', 'Double' and 'Integer'. I know one way of doing that is to write 3 functions with the same names, but it would be so tedious. Can anyone suggest any idea? Thank you. Public Function Square(x As Single) As Single Return x * x End Function 回答1: try following method Public Function Square(Of T)

Comparator for Optional<T>

我是研究僧i 提交于 2020-07-06 11:25:34
问题 I have abstract class OptionalComparator<T extends Comparable<T>> implements Comparator<Optional<T>> So far, so good. Following the model used by Optional itself, I figured it would be best to have a single instance of this class, and cast it when necessary (for example, to OptionalComparator<Integer> ). So I made private static final OptionalComparator<? extends Comparable<?>> ABSENT_FIRST . The trouble came when I tried to assign a value. What should the type be? new OptionalComparator