Posts

Showing posts with the label generics

Casting an object to a IList with an abstract type parameter

Image
Clash Royale CLAN TAG #URR8PPP Casting an object to a IList with an abstract type parameter In my code, I have an object: object obj = ... Which I know is of type MyListType, where MyListType is a subclass of IList and T is a subclass of MyAbstractClass. The type parameter of MyListType is constrained so that it is always a subclass of MyAbstractClass. However, trying to cast obj MyListType<MyAbstractClass> myList = (MyListType<MyAbstractClass>)obj; gives a runtime error. I tried to define an explicit operator public static explicit operator MyListType<MyAbstractClass>(MyListType<T> list) { MyListType<MyAbstractClass> newList = new MyListType<MyAbstractClass>(); foreach(T t in list) newList.Add((MyAbstractClass) t); return newList; } However, this operator doesn't get used by the cast since obj is an object, not a MyListType. Is there a workaround to successfully perform this cast? I've looked at this question, but their t...

Cannot instantiate the type Pair although not abstract

Image
Clash Royale CLAN TAG #URR8PPP Cannot instantiate the type Pair although not abstract Trying to write an iterator that returns a Pair: Part of my PairIterator public Pair next() { this.counter ++; Pair p = new Pair(this.l.get(counter - 1), this.l.get(counter)); //error occurs here } public class Pair<E> { private E e1; private E e2; public Pair(E e1, E e2) { this.e1 = e1; this.e2 = e2; } public E first() { return this.e1; } public E second() { return this.e2; } } getting Cannot instantiate the type Pair ... although Pair is not an abstract class/interface. Why is this happening? What is this.counter? No mention any where! – nagendra547 Aug 20 '17 at 16:08 I see no examples of 'new Pair' he...

A generic interface with type constraints accepting a default type

Image
Clash Royale CLAN TAG #URR8PPP A generic interface with type constraints accepting a default type I've created a simple generic interface in typescript interface DateAdapter<T> { clone(): T; } and I have a simple class which implements said interface class StandardDateAdapter implements DateAdapter<StandardDateAdapter> { clone: () => StandardDateAdapter; } I then have a generic Options interface which accepts a type extending DateAdapter . By default, I'd like the type argument to be StandardDateAdapter so I set it up like so: Options DateAdapter StandardDateAdapter interface Options<T extends DateAdapter<T> = StandardDateAdapter> { until?: T; } Unfortunately, typescript doesn't like this and is throwing an error Type 'StandardDateAdapter' does not satisfy the constraint 'DateAdapter<T>'. Types of property 'clone' are incompatible. Type '() => StandardDateAdapter' is not assignable to type ...

Generic method, equality constraint

Image
Clash Royale CLAN TAG #URR8PPP Generic method, equality constraint Hello Im trying to implement a generic method as controller base method, but the problem which I cannot understand happens with the generic method signature. <T> ResponseEntity<T> makeApiCall(String path, HttpMethod httpMethod, T body, boolean isAdmin){ String sender = isAdmin ? adminHash : userHash; HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", sender); headers.add("Content-Type", "application/json"); HttpEntity<T> entity = new HttpEntity<>(body,headers); ResponseEntity<T> responseEntity = restTemplate.exchange(path, HttpMethod.POST, entity, body.getClass()); return responseEntity; } The compile error I currently have is as follows: Incompatible equality constraint: T and capture of ? extends Object 2 Answers 2 ...

Cannot convert from 'method group' to 'System.Action' error

Image
Clash Royale CLAN TAG #URR8PPP Cannot convert from 'method group' to 'System.Action<object>' error I have created the following function: public void DelegatedCall(Action<Object> delegatedMethod) And defined the following method public void foo1(String str) { } However, when I try to call DelegateCall with foo1 : DelegateCall foo1 DelegatedCall(foo1); ...I get the following compiler error: Argument 1: cannot convert from 'method group' to 'System.Action<object>' Argument 1: cannot convert from 'method group' to 'System.Action<object>' What is the reason for this error and how can I correct it? Unfortunately, casting foo1 to Action is not an option. foo1 Action Related: What is a method group in C#? – DavidRR Jun 10 '16 at 12:57 3 A...

Generic class with class-bound constraint cannot be parametrized by class-bound protocol

Generic class with class-bound constraint cannot be parametrized by class-bound protocol I'd like to have a generic weak reference to an object and parametrize it by a protocol that is class-bound. Here is the code example that does not work in my case: protocol Protocol: class { // also written as `protocol Protocol: AnyObject {` func function() } final class A: Protocol { func function() {} } final class Weak<Type> where Type: AnyObject { final private weak var property: Type? init(property: Type) { self.property = property } } let a = A() let something = Weak<Protocol>(property: a) // error: 'Weak' requires that 'Protocol' be a class type I get an error on last line: 'Weak' requires that 'Protocol' be a class type . 'Weak' requires that 'Protocol' be a class type As Protocol will always be of class type (which is the same as AnyObject ) shouldn't that be allowed by the compiler? Is it p...

Java Parameter Generic interface

Java Parameter Generic interface i have a problem with parameter generic interface. I have 2 different interface and i need to pass the interface to constructor but the constructor gives warning. public interface FormFragmentContract { interface View extends BaseView { void updateWorkshopUI(int icon); void showRemoveFavoriteDialog(String wsId, String name); void favoriteOnClick(); void showSnackbar(); } } public interface WorkshopListContract { interface View extends BaseView { void initializeMap(); void onSuccessInitMap(); void favoriteOnClick(); void showSnackBar(String message); } } and the constructor code this gives warning like below workshopAdapter = new WorkshopAdapter(mActivity, workshopList, this); <- this give warning mismatch type public WorkshopAdapter(Context context, List<Workshop> workshops, //NEED TO BE GENERIC) { this.context = context; this.workshops = workshops; } How to make both of my interface generic ...