generics

Java. How to prohibit usage of raw generic type ?

Hi I'm writing the reporting system and want to make end-user specify type if he extends my class. class A <T extends C> {...} class B extends A // Compile error {...} class B extends A<D> // Compile error {...} class B extends A<C> // Success Is that possible? ...

Way to make Java parent class method return object of child class.

Is there any elegant way to make Java method located within parent class return object of child class, when this method is called from child class object? I want to implement this without using additional interfaces and extra methods, and to use this without class casts, auxiliary arguments and so on. Update: Sorry that I was not so c...

Is it possible to declare a generic event in vb.NET?

I have tried to write: Event EventName(Of T)() But Visual Studio tells me that "Type parameters cannot be specified on this declaration." Is such a declaration possible or will I have to put T as a regular argument? ...

Instantiating generic objects in Java

Is it possible to instantiate generic objects in Java as does in the following code fragment? I know that it is possible in C#. But, I haven not seen a similar mechanism yet in Java. // Let T be a generic type. T t = new T(); Thanks in advance. ...

A java method that has a generic parameter- why can't I pass an object with a generic parameter that is a subclass of the method arguments?

What I mean is, in the code below: class base { } class derived extends base { } class WTF { public void func(List<base> list) { } public void tryit() { func(new List<base>()); // ok func(new List<derived>()); // not ok } } But if the function simply took an object of base, it could take a derive...

Wrapping several different types in a common generic class for common access?

I have several web services which return various results. Some results are strings and some are arrays (automatically generated from the WSDL). When I call the web services, I want to get all the various results (including exceptions), and then operate on them through a common interface, but because of the type differences, I cannot ge...

Generic C# Code and the Plus Operator

I'm writing a class that does essentially the same type of calculation for each of the primitive numeric types in C#. Though the real calculation is more complex, think of it as a method to compute the average of a number of values, e.g. class Calc { public int Count { get; private set; } public int Total { get; private set; } ...

Why not always use Generics?

I am reading existing posts on Generics at SO. If Generics has so many advantages like Type safety, no overhead of boxing/unboxing and it is fast, why not always use it. Why to go for a non-generic thing? Edited (Question further extended below) I am a bit confused. The last time I read about Generics, months back, I read that if the T...

Weird problem about Java Generics operation

Hi all, The following code shows that I can insert uncompatible type into Map, but when I can not retrieve element from it. In the following example, I can put two integers into Map, but if I uncomment the last two lines, I will get ClassCastException. Is this bug of JDK, or I miss something, as I remember Java generic guarantees taht ...

How to access property of generic type in linq expression

Iam using .NET 3.5. I have asp.net mvc app. There is base controller: public abstract class BackendController<TModel> : BaseController where TModel : class { // skipped ... public ActionResult BatchDelete(int[] ids) { var entities = repository.GetList().Where(item => ids.Contains(item.ID)); repository.delete(entities) } ...

C#: Can I code public class MyGenericClass<T> where T:MyClass AND implement an Interface?

I see this: public class MyGenericClass<T> where T:IComparable { } and I have this: public class ProductRepository : IRepository<Product> { } How would you code something like this? public class ProductRepository<T> where T : Product : IRepository<Product> {} After all that, I thought I could simply make a single Repository clas...

C# Generics Inheritance

I have the following class public class AccountingBase<TItemType> where TItemType : AccountingItemBase And in my AccountingItemBase i have the following property: public virtual AccountingBase<AccountingItemBase> Parent { get; set; } in my AccountingBase, I am trying to do the following item.Parent = this; Logically this should ...

Java Generic - subtypes check?

How do I check generic subtypes passed as parameter? For example: public class A<T> { public T get();} public class AString extends A<String> {} public class ADate extends A<Date> {} public void checkparam(List<? extends A<?>> params) { ///Want to check if params is of type List<AString> or List<ADate> ? } Is it possible? What part ...

Using Java generics in an interface to enforce implementation of a method with the implementing type as a parameter

I have an interface like this: public interface DataObject { ... public void copyFrom(DataObject source); ... } And a class that implements it: public class DataObjectImpl implements DataObject { ... @Override public void copyFrom(final DataObject source) {...} public void copyFrom(final DataObjectImpl so...

nHibernate - Iterate properties of a class to generically add parameters for stored procedure

I want to have a class that defines my parameter data for a stored procedure. It's going to be a very simple object: public class MyQueryData : SprocObjectBase { public int Value1 { get; set; } public string Value2 { get; set; } public bool Value3 { get; set; } ... } It will inherit from a base class with shared values, like...

Keeping partially applied function generic

Is it possible to partially apply a function such as bprintf and prevent it from being restricted based on its initial use? I'd like to do the following: let builder = new System.Text.StringBuilder() let append = Printf.bprintf builder append "%i" 10 append "%s" string_value ...

Are nullable types stored on the stack?

If you have Nullable<int>, does this live on the stack? How many bytes does it take up? ...

One function implementing Generic and non-generic interface

Lets say I have a class, which implements a generic interface public interface IItem {} public interface IStuff<out TItem> where TItem : IItem { TItem FavoriteItem { get; } } public class MyStuff<TItem> : IStuff<TItem> where TItem : IItem { public TItem FavoriteItem { get { throw new NotImplementedException(); }...

Using Generics with Abstract Data Types in Java

For a CS class I need to solve an assigned problem using three data structures: Queue, PriorityQueue, and Stack. I wanted to write a single solution to the problem using an abstract data structure. I would implement the ADT with a wrapper class of each required data type. This is what I have so far: An interface called Method: public...

Type lists in C#'s generics: is it possible?

Hi All! Assuming there are following interfaces public interface IRoot { IChildA ChildA { get; } IChildB ChildB { get; } } public interface IChildA { IGrandChildA GrandChildA { get; } IGrandChildB GrandChildB { get; } } public interface IChildB { IGrandChildC GrandChildC { get; } } public interface IGrandChildA { } public i...