E
Ertugrul Söylemez
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.
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.