generics

Infer Generic Type in Class Method with Swift

白昼怎懂夜的黑 提交于 2020-02-01 08:49:11
问题 Is it possible for a generic method to infer its type based on the class in which it is being executed? I use CoreData NSManagedObject models to store and retrieve local data, and have managed to make everything generic in an easy to read and usable way, except for in one place. If a user wishes to query the local database to fetch a list of objects, he would write the following line: let posts: [Post] = Post.all() This will properly return "all" Post objects in the database, but the syntax

Castle Windsor resolving a generic interface

主宰稳场 提交于 2020-02-01 07:24:29
问题 I have generic interface with some implementations. interface IBaseInterface<in TObject, out TDetails> { TDetails GetDetails(TObject obj); } abstract class BaseImpl<TObject> : IBaseInterface<TObject, string> { public abstract string GetDetails(TObject obj); } class Impl0 : BaseImpl<decimal> { public override string GetDetails(decimal obj) { return "decimal"; } } class Impl1 : BaseImpl<string> { public override string GetDetails(string obj) { return "string"; } } class Impl2 : BaseImpl<Details

Invoke a function which is received as an interface variable in golang

六月ゝ 毕业季﹏ 提交于 2020-02-01 03:17:06
问题 I have a code which is similar to the following package main import "fmt" func PrintThis(arg string) { fmt.Printf("I'm printing %s", arg) } func PrintThisAndThat(arg1, arg2 string) { fmt.Printf("Now printing %s and %s", arg1, arg2) } func Invoke(fn interface{}, args ...string) { //fn(args...) } func main() { Invoke(PrintThis, "foo") Invoke(PrintThisAndThat, "foo", "bar") } This is not the actual production code, but this is a simplified version. Question :- If I uncomment the line //fn(args..

Why many Java Stream interface methods use lower bounded wildcard in parameters instead of generic type?

萝らか妹 提交于 2020-01-31 18:17:53
问题 Many Java Stream interface methods use lower bounded wildcard in parameters for example Stream<T> filter(Predicate<? super T> pred) and void forEach(Consumer<? super T> action) What is the advantage of using Predicate<? super T> over Predicate<T> here? I understand, with Predicate<? super T> as parameter, Predicate object of T and super types can be passed into the method but i can't think of a situation where a Predicate of super type needed over the specific type? For example if i have a

Covariance and Contravariance with Func in generics

断了今生、忘了曾经 提交于 2020-01-31 08:46:05
问题 I need more information about variance in generics and delegates. The following code snippet does not compile: Error CS1961 Invalid variance: The type parameter 'TIn' must be covariantly valid on 'Test.F(Func)'. 'TIn' is contravariant. public interface Test<in TIn, out TOut> { TOut F (Func<TIn, TOut> transform); } The .net Func definition is as follows: public delegate TResult Func<in T, out TResult> (T arg); Why the compiler complains about TIn being contravariant and TOut - covariant while

Any word on reified generics in Java?

╄→尐↘猪︶ㄣ 提交于 2020-01-31 08:42:31
问题 I know this question will probably provoke more discussion than concrete answers (which I know isn't preferable). But with the recent acquisition by Oracle, I was wondering if there's been any word that Java might (someday) get reified generics? I've heard that Oracle wants to give Java a bit of a boost, and I can think of no better way. 回答1: There's a good article on the discussion of reified generics, here, that you should read in regards to Java. Basically it outlines some of the pitfalls

Any word on reified generics in Java?

流过昼夜 提交于 2020-01-31 08:41:47
问题 I know this question will probably provoke more discussion than concrete answers (which I know isn't preferable). But with the recent acquisition by Oracle, I was wondering if there's been any word that Java might (someday) get reified generics? I've heard that Oracle wants to give Java a bit of a boost, and I can think of no better way. 回答1: There's a good article on the discussion of reified generics, here, that you should read in regards to Java. Basically it outlines some of the pitfalls

JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized

穿精又带淫゛_ 提交于 2020-01-30 20:14:33
问题 String[] boxOptions = {"1","2","4","8","16","20","40","100","400"}; JComboBox box = new JComboBox(boxOptions); I had these exact lines of code in my program before, and wasn't getting this error. I did a bit of searching and the results I found are going a bit over my head. Any ideas? The error is: JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized 回答1: You can use: JComboBox<String> box = new JComboBox<>(boxOptions); This happens because JComboBox is now

Find a specified generic DbSet in a DbContext dynamically when I have an entity

若如初见. 提交于 2020-01-30 19:36:49
问题 I have following classes and DbContext : public class Order:BaseEntity { public Number {get; set;} } Product:BaseEntity; { public Name {get; set;} } public class Context : DbContext { .... public DbSet<Order> Orders { set; get; } public DbSet<Product> Products { set; get; } .... } I have a list of objects that want to add to my context, too, but I don't know how can I find appropriate generic DbSet according each entity type dynamically. IList<BaseEntity> list = new List<BaseEntity>(); Order

How does java generics syntax help avoid type casting?

≯℡__Kan透↙ 提交于 2020-01-30 13:21:57
问题 Below is the code, import java.util.List; import java.util.ArrayList; public class Dummy { public static void main(String[] args) { List<String> lst = new ArrayList<String>(); lst.add("a string"); lst.add("another string"); String s = lst.get(0); } //end main } when the constructor new ArrayList<String>(); is invoked, array of type Object is created. .. lst holds Object[0] array. So, If array of type Object gets created by constructor, How does javac does not see type casting issue in this