Re: Base class referring to inheritor

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.


.



Relevant Pages

  • Re: Generic Collection Pattern
    ... That's not such a hot idea if you intend passing the container around cast ... as an interface or as an instance of the base class; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ada.Containers.Indefinite_Ordered_Maps of gcc 4.0.1 has bug ?
    ... Empty_Map: constant Map; ... No_Element: constant Cursor; ... function Length (Container: Map) return Count_Type; ... pragma Inline; ...
    (comp.lang.ada)
  • Re: Unification of Methods and Functions
    ... I am suggesting that we write factory methods using classmethod to give ... my classes all implement an interface which I'll call 'shape ... Rectangle and Ellipse have a common base class, ...
    (comp.lang.python)
  • Re: subroutine stack and C machine model
    ... I presented an interface proposal ... I used the term to refer to the common feature in OO languages of allowing ... interface "container", and be used interchangeably by code which relies on ... and that is why they are useful ABSTRACTIONS! ...
    (comp.lang.c)
  • Re: Abstract class or interface?
    ... I try to make the decision based on the relationship of the derived class to the base class. ... If the derived class "can act like" the type of the base class, I'd lean towards an interface. ... I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)