generics

Proper use of unbounded wilcard generics

主宰稳场 提交于 2020-01-06 04:31:09
问题 I'm trying to use unbounded wildcards because I have some Callable's that return an Object but I'm not interested in the object (they return null and I'm just using Callable instead of Runnable to be able to throw checked Exceptions). So in principle I want to do this: CompletionService<?> ecs = ... Callable<?> = ... Future<?> f = ecs.submit(c); But here I get an error saying: The method submit(Callable<capture#1-of ?>) in the type CompletionService<capture#1-of ?> is not applicable for the

Parameters generic of overloaded function doesn't contain all options

房东的猫 提交于 2020-01-06 04:29:19
问题 Given an overloaded function example . function example(a: string): number function example(a: string, b?: string): number { return 1 } type Result = Parameters<typeof example> I'd expect Result to contain ALL options for arguments of example , not just the first / top-most argument set. How can I get the parameters? 回答1: In the answers to the question this duplicates the limitation mentioned in @ford04's answer here, that infer only looks at the last overloaded signature, is acknowledged.

List of Generics T using Interface

本秂侑毒 提交于 2020-01-06 04:09:07
问题 Following these two SO questions: Is it possible to use a list of untyped generics in C#? and How am I able to create A List<T> containing an open generic Interface? public interface IValue { object Min { get; set; } object Max { get; set; } object Avg { get; set; } } public abstract class Value<T> : IValue { abstract T Min; abstract T Max; abstract T Avg; object IValue.Avg { get { return Avg; } } object IValue.Min { get { return Min; } } object IValue.Max { get { return Max; } } public Value

Using KeyValuePair in VB.NET for a reverse lookup capable dictionary (example hint in C# needs converting)

百般思念 提交于 2020-01-06 03:18:27
问题 I'm currently on a VB.NET project and wish to use a KeyValuePair to facilitate a reverse lookup. I've found a great example in C# here: http://www.dreamincode.net/forums/showtopic78080.htm, however I am having a small problem converting to VB.NET (both manually and using a translator (online carlosag)). For example the syntax I would expect in the Add method is as follows: Public Sub Add(ByVal key As TKey, ByVal value As TValue) Me.Add(New KeyValuePair(Of Tkey(key, value)) End Sub Whereas

Generic Function with (Of T as What?) to accept String, Integer, DateTime?

北战南征 提交于 2020-01-06 03:03:09
问题 I've got this Function: Public Class QueryStringUtil Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal queryStringVariable As String) As T Dim queryStringObject As Nullable(Of T) = Nothing If queryStringVariable <> Nothing Then If HttpContext.Current.Request.QueryString(queryStringVariable) IsNot Nothing Then queryStringObject = CTypeDynamic(Of T)(queryStringVariable) End If End If Return queryStringObject End Function End Class When I try to call it like this: Dim intTest

Storing list of generic class of derived objects

纵饮孤独 提交于 2020-01-06 01:36:50
问题 For example, how do I store a list of DataContainers that all use types that derive from the same base class. public class Animal {} public class Cat : Animal {} public class Dog : Animal {} public class DataContainer <TData> where TData : Animal { TData innerObject = new TData (); public TData GetData () { return innerObject; } } public class DataManager { static void Main () { DataContainer<Cat> CatData = new DataContainer<Cat> (); DataContainer<Dog> DogData = new DataContainer<Dog> (); var

How to annotate nested functions with generic type arguments

北战南征 提交于 2020-01-06 01:15:32
问题 Given a function that returns a factory, how can I annotate the function/factory so that it contains the correct type definitions? Here's my example: class item<TA, TB> { constructor(a: TA, b: TB) { this.a = a; } a: T } function generate(c) { return function a(a) { return function b(b) { return new c(a, b); } } } const factory = generate(item); // I want this to always be annotated as <TA, TB> (a: TA) => (b: TB) => item<TA, TB> const partial = factory('string'); // instance should now be of

Cannot convert from Foo<F> to F?

别等时光非礼了梦想. 提交于 2020-01-05 22:46:12
问题 I'm using a CRTP-style generic construct in C#, and have these classes: public abstract class Foo<F> where F : Foo<F> { public abstract Bar<F> Bar { get; } public void Baz() { return this.Bar.Qux(this); //Error is here: "Argument 1: Cannot convert from 'Foo<F>' to 'F' } } public abstract class Bar<F> where F : Foo<F> { public abstract void Qux(F foo); } This seems very strange to me, considering this should always be a Foo, right? 回答1: In that context, this is a Foo<F> . The argument of Qux

Understanding the Basics of Generics Sorting

时光毁灭记忆、已成空白 提交于 2020-01-05 20:28:50
问题 There are a number of question on Stack Overflow about sorting Generics; however, I am interested in sorting Generics in the simplest way: nothing nested. The code I have below is an attempt to sort a generic set - or, list - of elements. List<E> l = new LinkedList<>(arbSet); Collections.sort(l); arbSet is just a set of elements: Set<E> arbSet . Clearly, this is problematic - it shouldn't work. To make sure I know this, Eclipse gives me the following for my attempt to call .sort : Bound

@XmlElementWrapper with a generic List and inheritance

旧街凉风 提交于 2020-01-05 15:06:07
问题 I'd like to do the following: public abstract class SomeItems<E> { protected List<E> items; public void setItems(List<E> items) { this.items = items; } ....do other stuff with items.... } @XmlRootElement(name = "foos") public class Foos extends SomeItems<Foo> { @XmlElementWrapper(name="items") @XmlElement(name="foo") public List<Foo> getItems() { return this.items; } } @XmlRootElement(name = "bars") public class Bars extends SomeItems<Bar> { @XmlElementWrapper(name="items") @XmlElement(name=