Java generics incompatible types (no instance(s) of type variable(s) T exist)

ArrayList is an implementation of List interface. So all ArrayList instances are List instances but all List instances are not necessarily ArrayList. So when you call this method : public static <T> List<T> inRange(List<T> list, int index, int range) { you cannot assign its result to an ArrayList as you are doing : ArrayList<View> inRange … Read more

extracting data from typing types

Checking if a variable conforms to a typing object To check if string_list conforms to string_list_class, you can use the typeguard type checking library. from typeguard import check_type try: check_type(‘string_list’, string_list, string_list_class) print(“string_list conforms to string_list_class”) except TypeError: print(“string_list does not conform to string_list_class”) Checking the generic type of a typing object To check if … Read more

Restricting generic types to one of several classes in Typescript

I banged my head on this for a few hours, but the solution seems obvious in retrospect. First I present the solution, then I compare it to prior approaches. (Tested in Typescript 2.6.2.) // WORKING SOLUTION: union of types with type checks class MustBeThis { method1() { } } class OrThis { method2() { } … Read more

Typescript: infer type of generic after optional first generic

You want something like partial type parameter inference, which is not currently a feature of TypeScript (see microsoft/TypeScript#26242). Right now you either have to specify all type parameters manually or let the compiler infer all type parameters; there’s no partial inference. As you’ve noticed, generic type parameter defaults do not scratch this itch; a default … Read more

How to use generics and list of generics with json serialization in Dart?

json_serializable json_serializable has a several strategies1 to handle generic types as single objects T or List<T> (as of v. 5.0.2+) : Helper Class: JsonConverter Helper Methods: @JsonKey(fromJson:, toJson:) Generic Argument Factories @JsonSerializable(genericArgumentFactories: true) 1 Of which I’m aware. There’s likely other ways to do this. Helper Class: JsonConverter Basic idea: write a custom JsonConverter class … Read more