generics

Function Templates - Explicit specialisation vs Global Functions (C++)

I know that Function Templates are used so as to make the functions portable and so that they could be used with any data types. Also Explicit Specialization of templates is done if we have a more efficient implementation for a specific data type. But then instead of Explicit Specialization we could also just code a Nontemplate Functio...

generic locked pool, adding generic to non generic tlist

I tried my hand at a generic class, and on a second attempt I've tried to make a generic locked pool. I almost got it to work I stumble on the spot where I want to put a generic typed class into a locked tlist obtained from tthreadlist. The main question is: Does anybody know a solution to this problem? (see "problem spot" in the s...

How can I use Scala's Manifest class to instantiate the erased class at runtime?

I'm doing some WebDriver+PageObject stuff. (If your not familiar with PageObjects, this is a pattern where you have a class representing each page on your site which exposes all the functions of the page using the domain language, hiding the HTML stuff from the test.) I want to be lazy and have one 'submit' method in my abstract Page c...

I'm trying to mock Jersey WebResource with Mockito, and can't do it...

This is my code (Jersey 1.4 + Mockito 1.8.5): import org.junit.Test; import static org.junit.Assert.*; import com.sun.jersey.api.client.WebResource; import static org.mockito.Mockito.*; public FooTest { @Test public shouldMakeAHttpCall() { WebResource wr = mock(WebResource.class); doReturn(wr).when(wr).accept(anyVararg()); ...

an I prevent a specific type using generic restrictions

I have an overload method - the first implementation always returns a single object, the second implementation always returns an enumeration. I'd like to make the methods generic and overloaded, and restrict the compiler from attempting to bind to the non-enumeration method when the generic type is enumerable... class Cache { T Get...

Is there a good pattern for exposing a generic collection as readonly?

So I've got these classes that expose a collection of child objects. I don't want other classes adding or removing objects from collections because I need to wire into events in the child objects, so as they get added or removed I want to be able to do additional processing. But I really love the ease of manipulating generics internally...

Groovy static generic type

I've been playing around with getting rid of DAOs in favor of ActiveRecord like entities in Java, but generics won't allow me to have the full functionality that I want (like static finders). Somehow Groovy does, but I'm confused why. Given the following: class ActiveRecord<T> { static Class<T> genericType; ActiveRecord() { ...

Java Generic Object Passing

Hello Everybody! I'm playing around on GAE (I'm using Objectify) and wanted to make something like a generic method, but aint sure how to do it (and as far as my understandig goes, generics wouldn't be a solution for me). This is my setup: public abstract class Cloud{ Key<Cloud> parent; public Cloud(Key<Cloud> parent,...){ ...

Generic inheritance in java

In c++ we can write: #include <iostream> class Base1 { public: void test() { std::cout << "Base 1" << std::endl; } }; class Base2 { public: void test() { std::cout << "Base 2" << std::endl; } }; template<class T> class Derived: public T { }; int main() { Derived<Base1> d1; Derived<Base2> d2; d1.test(); d2.test()...

C#: Call non-generic method from generic method

class CustomClass<T> where T: bool { public CustomClass(T defaultValue) { init(defaultValue); // why can't the compiler just use void init(bool) here? } public void init(bool defaultValue) { } // public void init(int defaultValue) will be implemented later } Hello. This seems to be a simple question...

Compiler complains when I iterate Non-Generics Map in Java

Hi all, I meet weired problem when I iterate a Non-Generics Map in Java Map map=new HashMap(); for (Map.Entry entry:map.entrySet()){ } But compiler complains and says that "Type mismatch: cannot convert from element type Object to Map.Entry" When I change the Map type to Generics, it can work Map<Object,Object> map=new HashMap<Obje...

How to create class containing property of various types in C#

I'm trying to create a class to represent the value of a column in SQL. Here's the final goal I want to reach: public string GenerateInsertSql() { StringBuilder insertSql = new StringBuilder(); insertSql.Append("INSERT INTO " + SchemaName + "." + TableName + "\n"); insertSql.Append("(\n"); int counter = 0; foreach (ColumnValue...

C# cannot call overloaded non-generic method from generic method

I have some legacy code with a method foo which has 700+ overloads: [DllImport("3rdparty.dll")] protected static extern void foo(int len, ref structA obj); [DllImport("3rdparty.dll")] protected static extern void foo(int len, ref structB obj); [DllImport("3rdparty.dll")] protected static extern void foo(int len, ref structC obj); //and ...

Can I dictate that a C# type parameter must only be an interface type?

I would like to implement a generic C# class which looks roughly as follows: abstract class Foobar<T> : AbstractBase, T { ... } This fails because C# will only allow types after the base class to be interfaces, so next I try this: abstract class Foobar<T> : AbstractBase, T where T : interface { ... } But then I find that C# does no...

How to create a class that uses a generic type that allows nullable and non-nullable types.

I'm trying to create a class to use as a field in other objects that can hold a temporary unsaved value until a SaveChanges method is called on the object, so that I can pass the value to a stored proc, which will update any non-null fields with the new value, and leave fields with null as their original values. I want to be able to do ...

Is it possible to derive nested class from generic type provided by outer class?

I'd like to create a nested class which is based on the type provided to the outer class. I need the inner class to extend T by some members: TOuterClass<T:class> = class type TNestedClass = class(T) MoreData:Integer; end; end; The compiler says "No" or more specifically [DCC Error] MyUnit.pas(20): E2021 Class type req...

C# List<string> "contains" question

Is there an easy way, either through LINQ or Generics, to find out if elements in one List are all available in another List. I'm currently using Intersect to check this. For e.g. List<string> list1; //{ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 } List<string> list2; //{ 1, 3, 9 } list1.Contains(list2) == true Thanks in advance ...

Why no generics in go?

Disclaimer: I've only played with GO for one day now, so there's a good chance I've missed a lot. Does anybody know why there is no real support for generics/templates/whatsInAName in GO? So there is a generic map, but that's supplied by the compiler, while a GO programmer can't write her own implementation. With all the talk about maki...

Ienumerable Extension method for dictionary array calls

Hi All Im trying to get an enumberable collection from a dictionary held array. Or should I say, I'm trying to write an extension method for my dictionary objects which store arrays to return an IEnumerable item when the result is null. Im using dictionarys to store array datasets (there are speed reasons for this), which I extract at ...

Convert linq model to generic list

hi, I have an existing class Image which is used extensively throughout my application. I need to return a generic list of images (List) to the frontend but as there is no stored proc in the 3ed party DB I am quering I need to use Linq to Sql. I have create a dbtm file of the database I am quering in my DAL which looks like: ImageCat ...