covariance

Using Sub-Types And Return Types in Scala to Process a Generic Object Into a Specific One

I think this is about covariance but I'm weak on the topic... I have a generic Event class used for things like database persistance, let's say like this: class Event( subject: Long, verb: String, directobject: Option[Long], indirectobject: Option[Long], timestamp: Long) { def getSubject = subject def getVerb = verb def...

Understanding Covariant and Contravariant interfaces in C#

I've come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context. Is there a good concise explanation of what they are and what they are useful for out there? Edit for clarification: Covariant interface: interface IBibble<out T> . . Contravariant interface: int...

Using Covariance with an Interface Base Type in .NET 4?

I have some entities created with LINQ-to-SQL. Six of these entities (representing values primarily in drop-down lists) implement an interface I've called IValue. I did this because the UI layer is going to have to account for a couple special cases -- notably, what to display if the original value on a record has been flagged as deleted...

Question about C# 4.0's generics covariance

Having defined this interface: public interface IInputBoxService<out T> { bool ShowDialog(); T Result { get; } } Why does the following code work: public class StringInputBoxService : IInputBoxService<string> { ... } ... IInputBoxService<object> service = new StringInputBoxService(); and this doesn't?: public class I...

Covariance and Contravariance inference in C# 4.0

When we define our interfaces in C# 4.0, we are allowed to mark each of the generic parameters as in or out. If we try to set a generic parameter as out and that'd lead to a problem, the compiler raises an error, not allowing us to do that. Question: If the compiler has ways of inferring what are valid uses for both covariance (out) an...

.NET 4.0 Generic Invariant, Covariant, Contravariant

Here's the scenario i am faced with: public abstract class Record { } public abstract class TableRecord : Record { } public abstract class LookupTableRecord : TableRecord { } public sealed class UserRecord : LookupTableRecord { } public interface IDataAccessLayer<TRecord> where TRecord : Record { } public interface ITable...

Is this a covariance bug in C# 4 ?

In the following piece of code I expected to be able to implicitly cast from elements to baseElements because TBase is implicitly convertible to IBase. public interface IBase { } public interface IDerived : IBase { } public class VarianceBug { public void Foo<TBase>() where TBase : IBase { IEnumerable<TBase> elements = n...

Is is possible to do an end-run around generics covariance in C# < 4 in this hypothetical situation?

Suppose I have a small inheritance hierarchy of Animals: public interface IAnimal { string Speak(); } public class Animal : IAnimal { public Animal() {} public string Speak() { return "[Animal] Growl!"; } } public class Ape : IAnimal { public string Speak() { return "[Ape] Rawrrrrrrr!"; } } pub...

Is it possible to override a property and return a derived type in VB.NET?

Consider the following classes representing an Ordering system: Public Class OrderBase Public MustOverride Property OrderItem as OrderItemBase End Class Public Class OrderItemBase End Class Now, suppose we want to extend these classes to a more specific set of order classes, keeping the aggregate nature of OrderBase: Public Clas...

Can I have a type that's both, covariant and contravariant, i.e. fully fungible/changeable with sub and super types?

Just a stupid question. I could try it out in 2 minutes, really. It's just that I have 1 GB RAM and have already got 2 instances of VS 2010 open on my desktop, with an instance of VS 2005, too. Opening another instance of VS 2010 would be an over kill. Can I have a type (for now forgetting its semantics) that can be covariant as well as...

C# compiler fails to recognize a class is implementing an interface

The following code fails to compile (using VS2010) and I don't see why. The compiler should be able to infer that List<TestClass> is 'compatible' (sorry for lack of a better word) with IEnumerable<ITest>, but somehow it doesn't. What am I missing here? interface ITest { void Test(); } class TestClass : ITest { public void Te...

Detect variance on generic type parameters of interfaces

Is there a way to reflect on an interface to detect variance on its generic type parameters and return types? In other words, can I use reflection to differentiate between the two interfaces: interface IVariant<out R, in A> { R DoSomething(A arg); } interface IInvariant<R, A> { R DoSomething(A arg); } The IL for both looks the...

Is C# 4.0 Tuple covariant

(I would check this out for myself, but I don't have VS2010 (yet)) Say I have 2 base interfaces: IBaseModelInterface IBaseViewInterface And 2 interfaces realizing those: ISubModelInterface : IBaseModelInterface ISubViewInterface : IBaseViewInterface If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set t...

ref and out parameters in C# and cannot be marked as variant.

What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the followi...

Scala: A class declaring itself as a variable

I'm trying to make a binary tree in scala and need to make a method for it so I'm trying to make functions inside the class that deals with children and parent. I want to make the parent a Tree so that I can recursively call it in another function called getPath but I can't create a Tree inside the Tree class. this is the code: case cl...

Is there a way to determine the Variance of an Interface / Delegate in C# 4.0?

So now that we have generic Covariance and Contravariance on interfaces and delegates in C#, I was just curious if given a Type, you can figure out the covariance/contravariance of its generic arguments. I started trying to write my own implementation, which would look through all of the methods on a given type and see if the return type...

Why aren't classes in .NET 4 covariant?

Possible Duplicate: Why isnt there generic variance for classes in C# 4.0? As a rookie programmer I have a couple of questions about variance in .NET 4. Not so much about how it works, but why certain things are not variant and if other people would find this useful. Question 1: I know that interfaces and delegates can be c...

A good practical application of the Covariance and Contravariance in .NET 4.0?

I wish to get acquainted with the recently release of the .NET Framework 4.0 and its Covariance and Contravariance in Generics. Even though I have read what is written at the referenced link, I can't get a grab on how it should be used, and when it shouldn't. A brief explanation and a simple real-world-like code sample is appreciated. ...

EF/LINQ: Where() against a property of a subtype

I have a set of POCOs, all of which implement the following simple interface: interface IIdObject { int Id { get; set; } } A subset of these POCOs implement this additional interface: interface IDeletableObject : IIdObject { bool IsDeleted { get; set; } } I have a repository hierarchy that looks something like this:...

How is method group overload resolution different to method call overload resolution?

The following code doesn't compile (error CS0123: No overload for 'System.Convert.ToString(object)' matches delegate 'System.Converter<T,string>'): class A<T> { void Method(T obj) { Converter<T, string> toString = Convert.ToString; // this doesn't work either (on .NET 4): Converter<object, string> toString2 ...