generics

Integer type refrence String type value.

two class: public class BaseDo<K> { protected K id; public K getId() { return id; } public void setId(K id) { this.id = id; } } public class BeanDo extends BaseDo<Integer> { private String beanName; public String getBeanName() { return beanName; } public void setBeanName(...

Using parameter that implements multiple interfaces pre-generics.

Suppose I have these interfaces: public interface I1 { void foo(); } public interface I2 { void bar(); } and the classes: public class A extends AParent implements I1, I2 { // code for foo and bar methods here } public class B extends BParent implements I1, I2 { // code for foo and bar methods here } public class C extend...

Is this valid Java?

Is this valid Java? import java.util.Arrays; import java.util.List; class TestWillThatCompile { public static String f(List<String> list) { System.out.println("strings"); return null; } public static Integer f(List<Integer> list) { System.out.println("numbers"); return null; } publ...

Using anonymous generic types with multiple generics

After reviewing this blog by Kirill Osenkov (How to create a generic List of anonymous types?) I am trying to do something a little more advanced and having issues. The following code compiles: var anon = new { One = "1", Two = "2" }; var result = DoSomething(anon); public static T DoSomething<T>(T value) { ret...

From object to proper types: Is this the/a "right" way to work with this non-generified, object-returning API?

One particular aspect of some code I've written is causing me minor headaches, I can't pinpoint the reason - all the type checking and casting is making me feel uneasy. The code works as it is right now. I'd like to know wether or not there's a better way to handle the type-specific aspects of the code. I'm using a non-generified, objec...

Can I include a Generic type parameter in a lamba expression? (VB.NET 2010)

(Not really sure if I phrased the question correctly...) I want to create a lambda expression that would take an Object, attempt to convert it to a passed-in Type, and print to the console whether it was successful or not. At a glance, the lambda expression may seem a pretty silly way to accomplish this task, but I'd really like to kno...

Passing a reference to a TObjectDictionary<TKey, TValue>.TValueEnumerator

Hi All, I am trying to use Delphi 2010's TObjectDictionary generic. I would like to pass an enumerator of the Values property of that generic class, and the compiler doesn't seem to want to let me... Example: TAttributeStates = class(TInterfacedObject, IAttributeStates) private FStates: TObjectDictionary<TPatchAttribute, TAttr...

GetInterfaces() returns generic interface type with FullName = null

Can anyone explain to me why GetInterfaces() in the below code returns an interface type that has FullName = null? public class Program { static void Main(string[] args) { Type[] interfaces = typeof (Data<>).GetInterfaces(); foreach (Type @interface in interfaces) { Console.WriteLine("Name='{0...

Why does the following doesn't compile? (involves generics and inheritance in c#)

This compiles: class ReplicatedBaseType { } class NewType: ReplicatedBaseType { } class Document { ReplicatedBaseType BaseObject; Document() { BaseObject = new NewType(); } } But this does not: class DalBase<T> : where T: ReplicatedBaseType { } class Document...

Generics (List) typing question

I am trying to use a common technique to create objects from Xml. (Xml is legacy, so although there are already libraries to do this, it seemed faster to write this myself.) I don't understand the compiler's complaint about the generic usage. Code sample: public void createObjects() { List<Object1> objectOnes = new ArrayList<Object1...

Generics: Compiler seems incapable of recognizing that the type passed to an argument is the same as what is returned - why?

Let's say I have several POJOs which all extend a common supertype, BaseObject. I have a GenericDao which is declared as public interface GenericDao<T>. For each type-specific DAO, I have an interface which extends the generic type and restricts it to a concrete type (public interface UserDao extends GenericDao<User>) and then an impl...

Cast generic dictionary in C#.

Is there a way to cast a dictionary i a one-liner and without all the overhead in C# .net 3? var result = new Dictionary<string, AccessoryVariant>(); foreach (BaseVariant variant in mVariants.Values) { result.Add(variant.Id, (AccessoryVariant)variant); } return result; I would like to do something like this: return (Dictionary<st...

Is Dictionary<string,object> the best way to pass an unknown collection of variables to a constructor?

I have a number of "section items" (Lesson, Info) which inherit from the common type SectionItem. The various types of SectionItems share some but not all properties. I have found the best way to pass parameters to each kind of object is to pack them all in a Dictionary<string, object> and then let the base class SectionItem unpack the...

How to pass a raw type in Scala?

How can I pass a parameter to a Java method with a raw type in its method signature? My example API is as follows: class P<T> {} class Q { public void f(P p[]) {} } And my attempt to call it from Scala looks like this: val q = new Q() val p = new P() val p_array = Array(p) q.f(p_array) Which generates the following compiler e...

Selecting metadata using Linq and Reflection

Hi, Here's the situation: I'm attempting to get a collection of all types in my assembly that implement a specific generic interface along with the generic type parameters used. I have managed to put together a Linq query to perform this but it seems awfully redunant. I've read up on let and joins but couldn't see how to I'd use them t...

How do I make Align.vim \adec work correctly when java generics are involved?

The Align plugin is all nice and dandy, but I encounter problems with it when dealing with generics generics such that: HashMap<String, Object> session = new HashMap(); ArrayList<String> names = new ArrayList(); String banana = "Yo banana boy"; int count = 0; After \adec it becomes: HashMap<String, Object> session = new HashMap(); Ar...

Java: use generics in method return that differs from generic class parameter

class SomeClass<E> { Map<String, String> someMethod(); } And usage SomeClass a = new SomeClass(); a.someMethod().get("foo") // returns object, not string! //This code would not even compile String s = a.someMethod().get("foo"); But if I remove generalization (<E>) from SomeClass -- it works. It...

Problem with a method that accepts 2 lambdas

I have the following class: public class MyClass<T> where T : class { public void Method1<TResult>(T obj, Expression<Func<T, TResult>> expression) { //Do some work here... } public void Method2<TResult>(T obj, Expression<Func<T, TResult>> expression1, Expression<Func<T, TResult>>...

Weird generics issue

Hello Does anyone know why the following code does not compile and during compilation I get incompatible types exception ? public class Test<T> { public static void main(String[] args) { // using Test<?> solves the compilation error Test test = new Test(); // getting error incompatible types: // found : java.lang.Ob...

Class type inference from static method / caller?

Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) { // Do stuff with xyz... } } public class Ordering<TEntity> where TEntity : class { private readonly Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> Transform; private Ordering(Func<IQueryable<TEntity>,...