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.Collecti ons.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<TMedi ator, TView>
where TMediator : IMediator<TView >
{
// All views have a Mediator property.
TMediator Mediator { get;set;}
}
public class BaseView<TMedia tor, TView> : IBaseView<TMedi ator, 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<Media tor<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<Media tor, IView>
{
void ZoomChanged();
}
public class ConcreteView : BaseView<Mediat or, 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.ZoomChange d();
}
}
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<Medi ator<IView>,IVi ew>' 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<Media tor<IView>, IView>, so I don't understand why the
compiler complains about the conversion.
H^2 2 1524
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.Collecti ons.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<TMedi ator, TView> where TMediator : IMediator<TView > { // All views have a Mediator property. TMediator Mediator { get;set;} }
public class BaseView<TMedia tor, TView> : IBaseView<TMedi ator, 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<Media tor<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<Media tor, IView> { void ZoomChanged(); }
public class ConcreteView : BaseView<Mediat or, 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.ZoomChange d(); } }
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<Medi ator<IView>,IVi ew>' 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<Media tor<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<Media tor, IView>
Mediator : Mediator<IView>
it does not follow that
IView : IBaseView<Media tor<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 Generic types are not co-variant in their generic parameters, so from
Ok. Thanks for the response.
H^2 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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# generic use
constraint by type,complier is relaxed, but it is very limited to
programmer.Is there a way to get merits of both?
Maybe the following way can achieve this purpose :
//First add a keyword "constrant" to modify class or struct just...
|
by: Christophe |
last post by:
class A {}
class B {}
interface MyInterface
{
void method(A a);
void method(B b);
}
|
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 implements IQueue<Tfor
all types T (so it can make use of queues of various object types
internally). As useful as this is, it doesn't seem possible. The natural (but
illegal) notation would be something like
class A<QueueClasswhere QueueClass :...
|
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 the static Parse method of the conversion
class.
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |