Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler fails converting a constrained generic type

I have a class that has a Generic type "G"

In my class model i have

public class DetailElement : ElementDefinition

Let's say i have a method like this

        public void DoSomething<G>(G generic)
            where G : ElementDefinition
        {
            if (generic is DetailElement)
            {
                ((DetailElement)generic).DescEN = "Hello people"; //line 1
                //////
                ElementDefinition element = generic;
                ((DetailElement)element).DescEN = "Hello again"; //line 3
                //////
                (generic as DetailElement).DescEN = "Howdy"; //line 5
            }
            else
            {
                //do other stuff
            }
        }

Compiler reports one error in line 1:

Cannot convert type 'G' to 'DetailElement'

But line 3 works fine. I can workaround this issue by doing the code written in line 5.

What i would like to know is why does the compiler reports the error in line 1 and not the one in line 3, given that, as far as i know, they are identical.

edit: I am afraid i might be missing some important piece of the framework logic

edit2: Although solutions for the compiler error are important, my question is about why the compiler reports an error on line 1 and not in line 3.

like image 581
Luis Filipe Avatar asked Sep 03 '25 05:09

Luis Filipe


2 Answers

If G was constrained to be a DetailElement (where G : DetailElement) then you can go ahead and cast G to ElementDefinition, i.e., "(ElementDefinition) generic". But because G might be another subclass of ElementDefinition other than DetailElement at run-time it won't allow it at compile-time where the type is unknown and unverifiable.

In line 3 the type you cast from is known to be an ElementDefinition so all you're doing is an up-cast. The compiler doesn't know if it will be a succcesful cast at run-time but it will trust you there. The compiler is not so trusting for generics.

The as operator in line 5 might also return null and the compiler doesn't statically check the type to see if it's safe in that case. You can use as with any type, not just ones that are compatible with ElementDefinition.

From Can I Cast to and from Generic Type Parameters? on MSDN:

The compiler will only let you implicitly cast generic type parameters to object, or to constraint-specified types.

Such implicit casting is of course type safe, because any incompatibility is discovered at compile-time.

The compiler will let you explicitly cast generic type parameters to any interface, but not to a class:

   interface ISomeInterface {...}
   class SomeClass {...}
   class MyClass<T> 
    {
      void SomeMethod(T t)
       {
         ISomeInterface obj1 = (ISomeInterface)t;//Compiles
         SomeClass      obj2 = (SomeClass)t;     //Does not compile
       }
    }

However, you can force a cast from a generic type parameter to any other type using a temporary object variable

 void SomeMethod<T>(T t) 
  { object temp = t;
    MyOtherClass obj = (MyOtherClass)temp;  
  }

Needless to say, such explicit casting is dangerous because it may throw an exception at run-time if the concrete type used instead of the generic type parameter does not derive from the type you explicitly cast to.

Instead of risking a casting exception, a better approach is to use the is or as operators. The is operator returns true if the generic type parameter is of the queried type, and as will perform a cast if the types are compatible, and will return null otherwise.

public void SomeMethod(T t)
 {
   if(t is int) {...}

   string str = t as string;
   if(str != null) {...}
 }
like image 136
Mark Cidade Avatar answered Sep 04 '25 23:09

Mark Cidade


Generally, upcasting is a code smell. You can avoid it by method overloading. Try this:

public void DoSomething(DetailElement detailElement)
{
    // do DetailElement specific stuff
}

public void DoSomething<G>(G elementDefinition)
    where G : ElementDefinition
{
    // do generic ElementDefinition stuff
}

You can then take advantage of method overloading by using this code:

DetailElement foo = new DetailElement();

DoSomething(foo); // calls the non-generic method
DoSomething((ElementDefinition) foo); // calls the generic method
like image 32
Michael Meadows Avatar answered Sep 05 '25 00:09

Michael Meadows