generics

How do I read this Generics correctly from JSON?

我们两清 提交于 2021-01-28 03:51:35
问题 I'm reasonably confident in my first generics container, but stuck on how to word the casting on the client side. This is what was working before I got involved in learning <T> stuff: CommonNounContainer typeContainer = new Json().fromJson(CommonNounContainer.class, result); I was looking at having to create a different container for each class, and that doesn't seem like good design. Below is my updated, non-working attempt to read in my new generics container: JSONContainer<CommonNoun>

Can I restrict (compile or runtime) a generic to being an array [0..n] of char

守給你的承諾、 提交于 2021-01-28 03:30:36
问题 I have a program with a lot of structures defined as static arrays of char and records (usually consisting of arrays of char, but that's not so important). I am trying to create a generic interface for these structures, so they can be passed to a back-end C DLL. I am able to handle all types of records by using the <T: record> constraint, but array[0..n] of char falls foul of the 'non-nullable value type' rule. I can use unconstrained generics by declaring types for my different static arrays

Calling Method on Generic Type Java 8

一笑奈何 提交于 2021-01-28 03:10:38
问题 I have a function that accepts a parameter that has some generic type public < T extends SomeA > String foo(T t) {...} Now... SomeA contains a series of methods that can be called, like baz1() , baz2() , baz3() , etc... Now, here's the rub The variable T that will be passed into foo(T t) will be an instance of either SomeB or SomeC , where SomeB extends SomeA and SomeC extends someA . Both SomeB and SomeC contains a method glop(...) which returns SomeD , however , glop(...) is not in SomeA .

Casting generic class type to its base class fails [duplicate]

我怕爱的太早我们不能终老 提交于 2021-01-28 02:31:43
问题 This question already has answers here : How do i convert a class (which is derived from a generic “base” class) to that generic “base” class (4 answers) Why can't I assign a List<Derived> to a List<Base>? (5 answers) Closed 2 years ago . I am trying to cast a generic type to a non generic one that inherits from it but it fails. Check the code below: public class Subscription { public Subscription(DateTime expiration) { Expiration = expiration; } public DateTime Expiration { get; set; } }

Can I restrict (compile or runtime) a generic to being an array [0..n] of char

情到浓时终转凉″ 提交于 2021-01-28 02:14:11
问题 I have a program with a lot of structures defined as static arrays of char and records (usually consisting of arrays of char, but that's not so important). I am trying to create a generic interface for these structures, so they can be passed to a back-end C DLL. I am able to handle all types of records by using the <T: record> constraint, but array[0..n] of char falls foul of the 'non-nullable value type' rule. I can use unconstrained generics by declaring types for my different static arrays

The type parameter E is hiding the type E. Generics template to a class

两盒软妹~` 提交于 2021-01-28 02:05:39
问题 I am practicing generics and i did the following. Created a class called man as below. public class Man{ int hands=2; int legs=2; boolean alive; public Man(boolean b) { // TODO Auto-generated constructor stub alive=b; } } created a class called person public class Person<Man> { Man p; String name; public Person(Man p, String name) { this.p = p; this.name = name; } public Person() { // TODO Auto-generated constructor stub } } tried to instantiate a person of type string or int as below and it

Generic Linked List

梦想与她 提交于 2021-01-28 00:02:17
问题 Before posting my question, I would like to tell you that I have no prior experience in .Net technologies and have recently started to learn C# (and WPF). My company is looking to move onto .Net technologies and I am the only one in my team learning it, so have noone apart from you guys to discuss or ask something. So if my questions are too stupid or basic at best, please bear with me. I was trying to create a generic linked list class to allow creation of linked lists of different types. I

Typescript Map of Arbitrary Generics

僤鯓⒐⒋嵵緔 提交于 2021-01-27 23:21:41
问题 I'm trying to define two types, which should look something like: export type IQuery<P, U> = { request: string; params: (props: P, upsteam?: U) => object; key: (props: P, upstream?: U) => string; forceRequest: boolean; depends?: QueryMap } export type QueryMap = { [k: string]: IQuery }; The constraints I'm trying to express are that params and key have the same types for their two arguments, and that a QueryMap is just a mapping from a string to an arbitrary IQuery (doesn't matter what the

Specialize a type for generic function

早过忘川 提交于 2021-01-27 21:48:47
问题 Given this definition: declare function foo<T>(): { bar: T } // <T>() => { bar: T } type Foo = typeof foo; How can one provide specialization to the generic function by its type? What I want to achieve is to be able to do something like this: // { bar: number } type FooResult = ReturnType<Foo<number>>; But TypeScript complaints that Foo itself is not generic - the function it types is. 回答1: TypeScript doesn't really support the kind of higher-order typing you'd need to get this from

Can I add two generic values in java? [duplicate]

瘦欲@ 提交于 2021-01-27 21:27:44
问题 This question already has answers here : How to add two java.lang.Numbers? (10 answers) Closed 5 years ago . i have used instance of . But is there any other way to add two generic values. Can it be done like this? public static<T extends Number> T add(T x, T y){ T sum; sum=x + y; return sum; } 回答1: You could do this to support integers and doubles(and floats), though I don't see real value in it public static<T extends Number> T add(T x, T y){ if (x == null || y == null) { return null; } if