Re: Base class referring to inheritor
- From: Ertugrul Söylemez <es@xxxxxxxx>
- Date: Thu, 14 May 2009 16:40:08 +0200
Paul schrieb:
I suggest you first look at the thread started by Ivan 'How do I know type
of implementing object'.
Then I suggest you come explain below a little better. i got lost when
MyList started implemeting MySomething.
I particularly loved this line
An inheriting class should define the 'unpack' method such that
instances of Something<T> can become whatever is in a
Something<Something<T>> in some way that makes sense.
Sense not likely.......LOL...Tongue twister maybe...
Interfaces don't solve this either, because I can't refer to the
particular type implemented by the interface.
This is not entirely true. Create a asbtract method in the base class and
calling this method from the base type will call the method in the
implementing type. God ive confused myself now.
It seems like you are confusing this with OOP polymorphism. What I need
is type polymorphism. C# and other .NET languages provide that to some
extent, but not to the extent I'd like them to. I could use 'template
templates' in C++ for that, but I'd like to stick with C#, as I don't
like C++ syntax.
Any chance of a real world examle and not List/Somethign<T>
A less abstract example: The notion of mapping a function over whatever
a type contains. The following interface implements this idea:
interface Mappable<A> {
Mappable<B> map<B>(Func<A, B> f);
}
As its name suggests, the 'map' function is supposed to map a function
over whatever is contained in the Mappable. Consider a container, which
contains exactly one value. That container is a Mappable:
class OneValue<A> : Mappable<A> {
public A value;
public OneValue(v) { value = v; }
public Mappable<B> map<B>(Func<A, B> f) {
return new OneValue(f(value));
}
}
You could implement the 'map' function for any type, which can be
interpreted as a container.
However, here I'm facing the same problem: In the OneValue class, 'map'
does not return a OneValue, but a Mappable, so that I need type-casting
outside of the 'map' function. I'd like to get rid of this
type-casting, if that's possible, because it's messy and error-prone
(not only in this particular case, but casting is a bad idea in general).
Greets,
Ertugrul.
.
"Ertugrul S�ylemez" <es@xxxxxxxx> wrote in message
news:gugtq8$v1j$1@xxxxxxxxxxxxxxxxxx
Hello people,
I would like to write a base class in the following way:
abstract class Something<T> {
// ...
public abstract void unpack(Something<Something<T>>);
// ...
}
An inheriting class should define the 'unpack' method such that
instances of Something<T> can become whatever is in a
Something<Something<T>> in some way that makes sense.
Example: Where 'xs' is a list of type MyList<T> and and 'xss' is a list
of lists of type MyList<MyList<T>>, 'xs.unpack(xss)' replaces the list
contained by 'xs' with the concatenation of all lists contained by 'xss'
such that:
xs = someList
xss = {{10,11,12}, {14,17}, {19,20,22}}
// After xs.unpack(xss):
xs = {10,11,12,14,17,19,20,22}
xss = {{10,11,12}, {14,17}, {19,20,22}}
The problem is this: The MyList<T>.unpack method can be called with any
Something<Something<T>>, so I have to use explicit casting, which is
very ugly and not type-safe:
class MyList<T> : Something<T> {
// ...
public override void unpack(Something<Something<T>> cc) {
MyList<MyList<T>> xss = cc as MyList<MyList<T>>;
// ...
}
// ...
}
Is there an elegant way to solve this? There doesn't seem to be a way
to refer to more special classes from a base class.
Interfaces don't solve this either, because I can't refer to the
particular type implemented by the interface.
Any ideas?
Greets,
Ertugrul.
- References:
- Base class referring to inheritor
- From: Ertugrul Söylemez
- Re: Base class referring to inheritor
- From: Paul
- Base class referring to inheritor
- Prev by Date: Re: COM interop problem on 64bit OS
- Next by Date: Re: Windows Services and network printers
- Previous by thread: Re: Base class referring to inheritor
- Next by thread: Re: Base class referring to inheritor
- Index(es):
Relevant Pages
|