473,508 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic constraints: compiler errors on conversion that should bevalid

Howdy all,

I am getting a compiler error regarding a consrained conversion. It
complains that it can't make the type conversion, even though the
generic type argument inherits from the target of the conversion. I have
trimmed my source down as much as possible.

The classes implement something that behaves sort of like the Mediator
design pattern, but where the colleagues are abstract, and can be added
or removed on the fly. I guess that makes this more like the Observer
pattern. Either way, the base classes are designed to handle the plumbing.

The idea is that there is a mediator object that contains a list of view
objects. When something of interest happens in the mediator, it notifies
the views.

// main.cs
using System;
using System.Collections.Generic;

//---------------------------------------
// base types to handle plumbing
public interface IMediator<TView> //: IList<T>
{
// In reality, this interface inherits from IList<T>, but
// I removed that inheritance for brevity.
void Add(TView item);
void Clear();
}

public interface IBaseView<TMediator, TView>
where TMediator : IMediator<TView>
{
// All views have a Mediator property.
TMediator Mediator { get;set;}
}

public class BaseView<TMediator, TView> : IBaseView<TMediator, TView>
where TMediator : IMediator<TView>
{
public BaseView() {}

public TMediator Mediator
{
get { return m_Mediator; }
set { m_Mediator = value; }
}

protected TMediator m_Mediator;
}
public class Mediator<T> : IMediator<T>
where T : IBaseView<Mediator<T>, T>
{
protected List<T> m_List = new List<T>();

public virtual void Add(T item)
{
m_List.Add(item);
item.Mediator = this;
}
public virtual void Clear()
{
foreach(T t in m_List)
t.Mediator = null;
m_List.Clear();
}
}

//---------------------------------------
// concrete types
public interface IView : IBaseView<Mediator, IView>
{
void ZoomChanged();
}

public class ConcreteView : BaseView<Mediator, IView>, IView
{
public virtual void ZoomChanged() {}
}

public class Mediator : Mediator<IView> // ERROR on this line
{
public void ZoomTo(float zoom)
{
// ... zoom the display
foreach(IView view in m_List)
view.ZoomChanged();
}
}

class Program
{
static void Main(string[] args)
{
Mediator med = new Mediator();
med.Add(new ConcreteView());

med.ZoomTo(.5f);
}
}

// compiler error
Error 1 The type 'IView' must be convertible to
'IBaseView<Mediator<IView>,IView>' in order to use it as parameter 'T'
in the generic type or method 'Mediator<T>'

The confusing thing is that IView inherits from
IBaseView<Mediator<IView>, IView>, so I don't understand why the
compiler complains about the conversion.

H^2
Feb 27 '06 #1
2 1516
Harold Howe <hh***@gowebway.com> writes:
Howdy all,

I am getting a compiler error regarding a consrained conversion. It
complains that it can't make the type conversion, even though the
generic type argument inherits from the target of the conversion. I
have trimmed my source down as much as possible.

The classes implement something that behaves sort of like the Mediator
design pattern, but where the colleagues are abstract, and can be
added or removed on the fly. I guess that makes this more like the
Observer pattern. Either way, the base classes are designed to handle
the plumbing.

The idea is that there is a mediator object that contains a list of
view objects. When something of interest happens in the mediator, it
notifies the views.

// main.cs
using System;
using System.Collections.Generic;

//---------------------------------------
// base types to handle plumbing
public interface IMediator<TView> //: IList<T>
{
// In reality, this interface inherits from IList<T>, but
// I removed that inheritance for brevity.
void Add(TView item);
void Clear();
}

public interface IBaseView<TMediator, TView>
where TMediator : IMediator<TView>
{
// All views have a Mediator property.
TMediator Mediator { get;set;}
}

public class BaseView<TMediator, TView> : IBaseView<TMediator, TView>
where TMediator : IMediator<TView>
{
public BaseView() {}

public TMediator Mediator
{
get { return m_Mediator; }
set { m_Mediator = value; }
}

protected TMediator m_Mediator;
}
public class Mediator<T> : IMediator<T>
where T : IBaseView<Mediator<T>, T>
{
protected List<T> m_List = new List<T>();

public virtual void Add(T item)
{
m_List.Add(item);
item.Mediator = this;
}
public virtual void Clear()
{
foreach(T t in m_List)
t.Mediator = null;
m_List.Clear();
}
}

//---------------------------------------
// concrete types
public interface IView : IBaseView<Mediator, IView>
{
void ZoomChanged();
}

public class ConcreteView : BaseView<Mediator, IView>, IView
{
public virtual void ZoomChanged() {}
}

public class Mediator : Mediator<IView> // ERROR on this line
{
public void ZoomTo(float zoom)
{
// ... zoom the display
foreach(IView view in m_List)
view.ZoomChanged();
}
}

class Program
{
static void Main(string[] args)
{
Mediator med = new Mediator();
med.Add(new ConcreteView());

med.ZoomTo(.5f);
}
}

// compiler error
Error 1 The type 'IView' must be convertible to
'IBaseView<Mediator<IView>,IView>' in order to use it as parameter 'T'
in the generic type or method 'Mediator<T>'

The confusing thing is that IView inherits from
IBaseView<Mediator<IView>, IView>, so I don't understand why the
compiler complains about the conversion.


Generic types are not co-variant in their generic parameters, so
from

IView : IBaseView<Mediator, IView>
Mediator : Mediator<IView>

it does not follow that

IView : IBaseView<Mediator<IView>, IView>

Peter
--
Department of Natural Sciences http://www.dina.kvl.dk/~sestoft/
Royal Veterinary and Agricultural University * Tel +45 3528 2334
Thorvaldsensvej 40, DK-1871 Frederiksberg C, Denmark * Fax +45 3528 2350
Feb 27 '06 #2
Generic types are not co-variant in their generic parameters, so
from


Ok. Thanks for the response.

H^2
Mar 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2033
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
2
15721
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
9
12775
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
9
5830
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.