Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic interface problem

So here is my problem:

I have the interface:

public interface ICell<Feature> 
where Feature: struct, IComparable<ICell<Feature>>
{
    List<ICell<Feature>> Window { get; set; }
    Feature              GenusFeature { get; set; }
    Double               VitalityRatio { get; set; }
    String               PopulationMarker { get; set; }
    Boolean              Captured { get; set; }
}

And wanted to implement ISubstratum interface in this way:

public interface ISubstratum<K,T> : IDisposable 
where K : IDisposable
where T : struct
{
    ICell<T> this[Int32 i, Int32 j] { get; set; }
}

But compiler says that:

The type 'T' cannot be used as type parameter 'Feature' in the generic type or method 'Colorizer.Core.ICell<Feature>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.IComparable<Colorizer.Core.ICell<T>>'.

In some possible ISubstratum implementation I planned to pass a Bitmap as K && ICell (extented pixel info) as T.

How to resolve this?

Thanks!

like image 691
lexeme Avatar asked Sep 19 '26 06:09

lexeme


1 Answers

Basically you've got to have an extra constraint on T:

where T : struct, IComparable<ICell<T>>

then it should work fine. That's required to satisfy the same constraint on Feature in ICell<Feature>.

I would also suggest you rename the type parameter Feature to TFeature to make it more obviously a type parameter.

like image 185
Jon Skeet Avatar answered Sep 21 '26 20:09

Jon Skeet