Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic interfaces

Here is my code

public interface ITranslator<E, R>
{       
    E ToEntity<T>(R record);
}

class Gens : ITranslator<string, int>
{
    #region ITranslator<string,int> Members

    public string ToEntity<MyOtherClass>(int record)
    {
        return record.ToString();
    }

    #endregion
}

When I compile this, I get an error Type parameter declaration must be an identifier not a type

Why is that I cannot have ToEntity<MyOtherClass> but can only have ToEntity<T> ??

Edit: what is MyOtherClass doing ? I am converting between entities(POCOs equivalent of Entity framework) and record(Object returned by the framework) for multiple tables/classes. So I would want to use this to do my class specific conversion

like image 437
ram Avatar asked Jan 29 '10 23:01

ram


People also ask

What are generic interfaces?

A generic interface is primarily a normal interface like any other. It can be used to declare a variable but assigned the appropriate class. It can be returned from a method. It can be passed as argument.

Can interfaces be generic in Java?

Java Generic Interface In similar way, we can create generic interfaces in java. We can also have multiple type parameters as in Map interface. Again we can provide parameterized value to a parameterized type also, for example new HashMap<String, List<String>>(); is valid.

What is the use of generic interface in Java?

Generics make a class, interface and, method, consider all (reference) types that are given dynamically as parameters. This ensures type safety. Generic class parameters are specified in angle brackets “<>” after the class name as of the instance variable.

How do you write a generic interface?

The general syntax to declare a generic interface is as follows: interface interface-name<T> { void method-name(T t); // public abstract method. } In the above syntax, <T> is called a generic type parameter that specifies any data type used in the interface.


3 Answers

Your interface has a generic ToEntity<T> method that you've made non-generic in your implementation class Gens as ToEntity<MyOtherClass>. (A generic method could take any type parameter, possibly given certain constraints on T. Your Gens class is trying to provide a definition for ToEntity only for the type parameter MyOtherClass, which defeats the purpose of generics.)

In your code example, it's unclear how your Gens class is trying to use the MyOtherClass type; it's certainly not involved in the logic of ToEntity. We'd need more information to be able to guide you further.

To illustrate, here's what your current definition of the ITranslator<E, R> interface offers, in plain English:

"I provide a mechanism to translate any record of type R into an entity of type E, this mechanism being dependent upon any user-specified type T."

Your Gens class, on the other hand, the way it's currently designed, "implements" the above interface like so:

"I can translate integers to strings. I provide the illusion of allowing the user to specify a type to control how this translation is performed, but in fact there is no choice of type. The MyOtherClass class is involved somehow; that's all I can say."

From these two descriptions, it's clear that the Gens class is not really doing what the ITranslator<E, R> interface guarantees. Namely, it is not willing to accept a user-specified type for its ToEntity method. That's why this code won't compile for you.

like image 53
Dan Tao Avatar answered Sep 19 '22 11:09

Dan Tao


You must declare a constraint on a generic type.

public string ToEntity<T>(int record) where T : MyOtherClass
like image 23
Joel Etherton Avatar answered Sep 20 '22 11:09

Joel Etherton


That compiles OK for me in LINQpad. Maybe you have a type named E, R, or T somewhere?

Ahh I see what you're trying to do... you have MyOtherClass defined as a class somewhere yet you're trying to use it as a type argument in ToEntity. How exactly do you want MyOtherClass involved in ToEntity?

like image 24
Josh Avatar answered Sep 19 '22 11:09

Josh