generics

Why are the angle brackets before the return type omitted sometimes from the definition of a generic method

♀尐吖头ヾ 提交于 2021-02-04 15:09:30
问题 I was reading Effective Java chapter 5 about generics, in particular the items on preferring generic methods. I noticed that sometimes the type parameter(between angle brackets) in the method declaration before the return type is sometimes omitted. There are many cases like that, but for example on page 135 of the second edition: public void popAll(Collection<E> dst) { while (!isEmpty()) dst.add(pop()); } On the other hand, I have seen similar generic methods with the declaration public <E>

Cannot apply Operator '<' to operands of type T and T

走远了吗. 提交于 2021-02-04 12:12:00
问题 so here's my code public void BubbleSort<T>(T[] array) where T : IComparable<T> { for (int i = 0; i < array.Length; i++) { for (int j = 1; j < array.Length; j++) { if (array[j] < array[j - 1]) { } } } } and before you shoot be down for not searching. I have searched and one of the answers here on SO said to use an icomparable interface to solve the problem. unfortunately i am not going anywhere with this error. 回答1: It looks like you're expecting the IComparable<T> constraint to allow you to

Cannot apply Operator '<' to operands of type T and T

别说谁变了你拦得住时间么 提交于 2021-02-04 12:11:05
问题 so here's my code public void BubbleSort<T>(T[] array) where T : IComparable<T> { for (int i = 0; i < array.Length; i++) { for (int j = 1; j < array.Length; j++) { if (array[j] < array[j - 1]) { } } } } and before you shoot be down for not searching. I have searched and one of the answers here on SO said to use an icomparable interface to solve the problem. unfortunately i am not going anywhere with this error. 回答1: It looks like you're expecting the IComparable<T> constraint to allow you to

How to use method with generics and inheritance?

谁都会走 提交于 2021-02-04 08:21:20
问题 Having the following classes: public interface Step<C extends Config> { void setConfig(C config); } and public class ValidationStep implements Step<ValidationConf> { public void setConfig(ValidationConf conf) {} // implementation } and public class ProcessStep implements Step<ProcessConf> { public void setConfig(ProcessConf conf) {} // implementation } and public interface Config { Class<? extends Step> type(); } and public class ValidationConf implements Config { public Class<? extends Step>

Self referencing generic member in struct

大兔子大兔子 提交于 2021-02-04 08:01:46
问题 I need to broaden my understanding of how structs are compiled when using generics. I have the following code, which works public struct TestStruct { public GenericStruct<SecondTestStruct> Header; public int TestValue; } public struct GenericStruct<T> { public int MessageSize => System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); } public struct SecondTestStruct { public int TestValue; } static void Main(string[] args) { TestStruct test = new TestStruct(); Console.WriteLine($"Size of

Defining an array of differing generic types in TypeScript

笑着哭i 提交于 2021-02-04 06:12:49
问题 interface Instruction { promise: Promise<unknown>, callback?: ($html: JQuery, data: unknown ) => void } const arr: Instruction[] = [ { promise: Promise.resolve({ foo: 'bar' }), callback: ($html, data) => console.log(data.foo) }, { promise: Promise.resolve({ bar: 'foo' }), callback: ($html, data) => console.log(data.bar) } ]; Given the above, I'd like TypeScript to recognise that the data parameter in the callback function is of the same type as the resolution of the Promise. If it was stand

Defining an array of differing generic types in TypeScript

陌路散爱 提交于 2021-02-04 06:10:51
问题 interface Instruction { promise: Promise<unknown>, callback?: ($html: JQuery, data: unknown ) => void } const arr: Instruction[] = [ { promise: Promise.resolve({ foo: 'bar' }), callback: ($html, data) => console.log(data.foo) }, { promise: Promise.resolve({ bar: 'foo' }), callback: ($html, data) => console.log(data.bar) } ]; Given the above, I'd like TypeScript to recognise that the data parameter in the callback function is of the same type as the resolution of the Promise. If it was stand

Defining an array of differing generic types in TypeScript

风流意气都作罢 提交于 2021-02-04 06:09:53
问题 interface Instruction { promise: Promise<unknown>, callback?: ($html: JQuery, data: unknown ) => void } const arr: Instruction[] = [ { promise: Promise.resolve({ foo: 'bar' }), callback: ($html, data) => console.log(data.foo) }, { promise: Promise.resolve({ bar: 'foo' }), callback: ($html, data) => console.log(data.bar) } ]; Given the above, I'd like TypeScript to recognise that the data parameter in the callback function is of the same type as the resolution of the Promise. If it was stand

Writing a generic function that takes an iterable container as parameter in Rust

笑着哭i 提交于 2021-02-02 07:34:01
问题 I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec , BTreeSet , etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not possible to directly take an iterator as parameter and I also can't introduce any lifetime parameters to the function signature. Context I tried to implement the observer pattern in Rust. The observable and the observer look as follows: struct

Writing a generic function that takes an iterable container as parameter in Rust

喜你入骨 提交于 2021-02-02 07:32:35
问题 I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec , BTreeSet , etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not possible to directly take an iterator as parameter and I also can't introduce any lifetime parameters to the function signature. Context I tried to implement the observer pattern in Rust. The observable and the observer look as follows: struct