generics

Serious generics issue: int vs. Integer java

眉间皱痕 提交于 2020-03-05 01:31:52
问题 for a long while, i have been trying to get a piece of code to work, and it just is not possible. the class i have requires a generic which i have set to Integer. so i tried the follwoing: public class a<T> //set generics for this function { private T A; protected boolean d; public a(final T A) { this.A = A; //do calculations //since constructor cannot return a value, pass to another function. this.d = retnData(Integer.parseInt(A.toString()) != 100); //convert to an integer, the backwards way

Serious generics issue: int vs. Integer java

☆樱花仙子☆ 提交于 2020-03-05 01:31:35
问题 for a long while, i have been trying to get a piece of code to work, and it just is not possible. the class i have requires a generic which i have set to Integer. so i tried the follwoing: public class a<T> //set generics for this function { private T A; protected boolean d; public a(final T A) { this.A = A; //do calculations //since constructor cannot return a value, pass to another function. this.d = retnData(Integer.parseInt(A.toString()) != 100); //convert to an integer, the backwards way

How to make this Strategy-Object pattern type safe

南楼画角 提交于 2020-03-04 19:42:47
问题 This is the long version of a question I asked earlier For the tl;dr version please see here: Link I am sorry for this wall of text, but please bear with me. I put a lot of effort into the question and I believe the problem at hand should interesting to many here. Background I am writing a UI Framework with a classic Scene Graph. I have an abstract Top-Level class called Component and many subclasses, some of which are concrete while others abstract as well. A concrete subclass might be

How to make this Strategy-Object pattern type safe

瘦欲@ 提交于 2020-03-04 19:41:06
问题 This is the long version of a question I asked earlier For the tl;dr version please see here: Link I am sorry for this wall of text, but please bear with me. I put a lot of effort into the question and I believe the problem at hand should interesting to many here. Background I am writing a UI Framework with a classic Scene Graph. I have an abstract Top-Level class called Component and many subclasses, some of which are concrete while others abstract as well. A concrete subclass might be

Tuple to Object in TypeScript via generics

隐身守侯 提交于 2020-03-03 06:00:36
问题 I'm attempting to transform a Tuple union in TypeScript to an object, without losing any types. Here is an example of how it would work: type Tuples = ["foo", string] | ["bar", boolean] | ["baz", null]; /* ideally the type would be: { foo: string; bar: boolean; baz: null; } */ type AsObject = DoSomething<Tuples>; A simple solution to the above would be: type TupleToObject<T extends [string, any]> = { [key in T[0]]: T[1] }; /* type is: { foo: string | boolean | null; bar: string | boolean |

Tuple to Object in TypeScript via generics

此生再无相见时 提交于 2020-03-03 05:59:08
问题 I'm attempting to transform a Tuple union in TypeScript to an object, without losing any types. Here is an example of how it would work: type Tuples = ["foo", string] | ["bar", boolean] | ["baz", null]; /* ideally the type would be: { foo: string; bar: boolean; baz: null; } */ type AsObject = DoSomething<Tuples>; A simple solution to the above would be: type TupleToObject<T extends [string, any]> = { [key in T[0]]: T[1] }; /* type is: { foo: string | boolean | null; bar: string | boolean |

JDK1.5新特性一之泛型(Generics)

孤街醉人 提交于 2020-02-29 19:59:17
有人会疑问,泛型是什么东东? 引用官方的话来说: 泛型就是:“the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting”。看不明白吗??没关系,下面我就给大家解释一下我的理解(注:是我的理解,如果你表达的更好,欢迎指正~)。 说白了,jdk中加入了泛型,可以让我们省去类型强制转换的工作,同时加到编译时异常中来;再说白一点,就是你使用了泛型之后,不用再做强制类型转换了,同时如果你的赋值对象不匹配,那么编译器会警告你这里出错了,而不用再等到程序运行时报了一个ClassCastException的时候才发现代码有问题了。不知道我说的这么白白了,你明白了没有,还没有明白,那请往下看.... 比如,之前我们想要在操作集合类中的对象是这样写: static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ){

React with Typescript — Generics while using React.forwardRef

ⅰ亾dé卋堺 提交于 2020-02-28 06:52:05
问题 I am trying to create a generic component where a user can pass the a custom OptionType to the component to get type checking all the way through. This component also required a React.forwardRef . I can get it to work without a forwardRef. Any ideas? Code below: WithoutForwardRef.tsx export interface Option<OptionValueType = unknown> { value: OptionValueType; label: string; } interface WithoutForwardRefProps<OptionType> { onChange: (option: OptionType) => void; options: OptionType[]; } export

Why is 'box' instruction emitted for generic?

柔情痞子 提交于 2020-02-27 15:57:11
问题 Here is fairly simple generic class. Generic parameter is constrained to be reference type. IRepository and DbSet also contain the same constraint. public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity { protected readonly DbSet<TEntity> _dbSet; public void Insert(TEntity entity) { if (entity == null) throw new ArgumentNullException("entity", "Cannot add null entity."); _dbSet.Add(entity); } } Compiled IL contains box instruction. Here is the release version

Any VBNET equivalence of C# where generic constraint keyword?

人走茶凉 提交于 2020-02-27 15:09:05
问题 First, I wish to write myself a generic type for operations against the underlying Active Directory. For those of you who know about AD and the System.DirectoryServices namespace, the DirectoryEntry class is the top most important along with the DirectorySearcher class. When speaking the AD language, everything is a DirectoryEntry. With that said, my application needs to manage Users, Groups and Organizational Units (OU). Each of these objects are AD entries. Then, this sounds to me a good