generics

Java - A method that takes vararg and returns arraylist?

拜拜、爱过 提交于 2021-01-27 04:17:17
问题 I'm not entirely comfortable with generics and thus haven't found a solution to this yet. I have these three methods: public static List<ObjectA> objectAAsList(ObjectA ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectB> objectBAsList(ObjectB ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectC> objectCAsList(ObjectC ... items) { return new ArrayList<>(Arrays.asList(items)); } How can I create a single method that takes a

Java - A method that takes vararg and returns arraylist?

半世苍凉 提交于 2021-01-27 04:16:50
问题 I'm not entirely comfortable with generics and thus haven't found a solution to this yet. I have these three methods: public static List<ObjectA> objectAAsList(ObjectA ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectB> objectBAsList(ObjectB ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectC> objectCAsList(ObjectC ... items) { return new ArrayList<>(Arrays.asList(items)); } How can I create a single method that takes a

How to create Generics Pooling System for components/scripts?

喜夏-厌秋 提交于 2021-01-27 03:52:36
问题 My awareness of Generics is that they can help me streamline my pooling, but can't figure out how. My pooling system is minimalistic, but messy. And now getting unwieldy and messy, and MESSY. It doesn't scale nicely... My FXDistribrutor.cs class is a component attached to an object in the initial scene, designed to permanently exist throughout all scenes of the game. It has a static reference to itself, so that I can call into it from anywhere easily. More on that contrivance at the end. I'm

Type declaration based on type parameters in inner classes

▼魔方 西西 提交于 2021-01-27 02:22:09
问题 Does Java shadow type parameters? I am finding it hard to test for myself because Java generics do not get reified at run time. For example, given this code: public class NestedGeneric<T> { private InnerGeneric<T> innerGenericInstance; private static class InnerGeneric<T> { public T innerGenericField; } NestedGeneric() { innerGenericInstance = new InnerGeneric<T>(); } } Both the below statements compile fine: NestedGeneric<Integer> test1 = new NestedGeneric<Integer>(); NestedGeneric

“ambiguous use” on generic method after migration to swift 4

若如初见. 提交于 2021-01-26 22:48:20
问题 I am trying to migrate my code from xcode 8.2 swift 3.0.2 to xcode 9 swift 4, and I have problem with this code: func test<T0, TRet>(_ fn: (T0) -> TRet) -> Void { print("foo1") print(T0.self) } func test<T0, T1, TRet>(_ fn: (T0, T1) -> TRet) -> Void { print("foo2") print(T0.self) print(T1.self) } let fn2 : (Int, Int) -> Int = { (x:Int, y:Int)->Int in return x+y } test(fn2) xcode 8.0.2, swift 3.0.2 results with: foo2 Int Int xcode 9, swift 4 results with: Playground execution failed: error:

“ambiguous use” on generic method after migration to swift 4

点点圈 提交于 2021-01-26 22:47:43
问题 I am trying to migrate my code from xcode 8.2 swift 3.0.2 to xcode 9 swift 4, and I have problem with this code: func test<T0, TRet>(_ fn: (T0) -> TRet) -> Void { print("foo1") print(T0.self) } func test<T0, T1, TRet>(_ fn: (T0, T1) -> TRet) -> Void { print("foo2") print(T0.self) print(T1.self) } let fn2 : (Int, Int) -> Int = { (x:Int, y:Int)->Int in return x+y } test(fn2) xcode 8.0.2, swift 3.0.2 results with: foo2 Int Int xcode 9, swift 4 results with: Playground execution failed: error:

public static (const) in a generic .NET class

青春壹個敷衍的年華 提交于 2021-01-26 03:47:20
问题 Is there a syntax trick to get to the constant in a generic class without specifying an (ad-hoc) type? public class MyClass<T>{ public const string MyConstant = "fortytwo"; } // I try to avoid this type specification. var doeswork = MyClass<object>.MyConstant; // Syntax similar to what I'd like to accomplish. var doesnotwork = MyClass.MyConstant; There is a caveat about the static variable (constant) not being shared between different types like MyClass<object> and MyClass<int> but my

Why does VS warn me that typeof(T) is never the provided type in a generic method where the type parameter is restricted to implement T?

给你一囗甜甜゛ 提交于 2021-01-26 03:21:19
问题 I hope the question is correct, so let's give you an example. Imagine the following generic method: public abstract class Base : IDisposable { public static IEnumerable<T> GetList<T>() where T : Base { // To ensure T inherits from Base. if (typeof(T) is Base) throw new NotSupportedException(); // ... } } According to the MSDN the keyword where restricts the type parameter T to be of type Base or to inherit from this class. [...] a where clause can include a base class constraint, which states

Why does VS warn me that typeof(T) is never the provided type in a generic method where the type parameter is restricted to implement T?

馋奶兔 提交于 2021-01-26 03:17:31
问题 I hope the question is correct, so let's give you an example. Imagine the following generic method: public abstract class Base : IDisposable { public static IEnumerable<T> GetList<T>() where T : Base { // To ensure T inherits from Base. if (typeof(T) is Base) throw new NotSupportedException(); // ... } } According to the MSDN the keyword where restricts the type parameter T to be of type Base or to inherit from this class. [...] a where clause can include a base class constraint, which states

Generic typescript: Generic type from keyof T values

*爱你&永不变心* 提交于 2021-01-21 11:53:01
问题 How can I infer the result type (TTarget) from TSource and the given property names (keyof TSource)? I've the following function to copy defined properties to a new object: export declare type PropertyNamesOnly<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]; CopyProps<TSource, TTarget>(source: TSource, ...props: PropertyNamesOnly<TSource>[]): TTarget { const result: any = {}; for (const prop of props) { result[prop] = source[prop]; } return result; } Now I can use it like