Simple (?) generics question

O

Olaf Krumnow

Hi,

I have a requirement that, I thought, was very simple, but in fact
turned out as impossible for me.

I read some posts here and the language specs from MS, but couldn't
get a solution.

I coded the part in java, because it's very straightforward there:

public class Test {
void test(){
IOption<Color> iOpt = new Option<Color>(Color.BLACK);
IOption<Font> fOpt = new Option<Font>(Font.getFont("xyz"));
List<IOption<?>> list = new ArrayList<IOption<?>>();
list.add(iOpt);
list.add(fOpt);
for (IOption<?> option : list) {
System.out.println(option.result().toString());
}
}
}


It creates a list of options and adds some of different kind. Finally
it iterates the list and prints the string representation of all
results. The interface IOption and the class Option are very simple
for demonstration purposes:

public interface IOption<T> {
T result();
}

public class Option<T> implements IOption<T> {

private T _val;

public Option(T val) {
_val = val;
}

public T result() {
return _val;
}
}


I couldn't get this to work in C# because I didn't find that wildcard-
thingy of java. What am I missing? Or isn't it possible using
generics? Any idea?

Thanks in advance,

Olaf
 
J

Jon Skeet [C# MVP]

I couldn't get this to work in C# because I didn't find that wildcard-
thingy of java. What am I missing? Or isn't it possible using
generics? Any idea?

It's not possible - IOption<Foo> and IOption<Bar> are effectively
completely different types.

What you *could* do is make IOption<T> also implement a non-generic
IOption interface, and create a List<IOption>.

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

To add to this, it's not something that is likely to be put into .NET
anytime soon. I spoke with some people at MS (I even asked Mads Torgersen
about it) at the last MVP summit (and the one before that), and they keep
tip toeing around it. Granted, there isn't a great deal of clamor for it
(which is why they aren't interested in putting it in right now), but it
would be interesting, to say the least.
 
J

Jon Skeet [C# MVP]

To add to this, it's not something that is likely to be put into .NET
anytime soon. I spoke with some people at MS (I even asked Mads Torgersen
about it) at the last MVP summit (and the one before that), and they keep
tip toeing around it. Granted, there isn't a great deal of clamor for it
(which is why they aren't interested in putting it in right now), but it
would be interesting, to say the least.

Well, it's already available in .NET itself to some extent - the CLR
understands covariant/contravariant interfaces, but they're not
exposed in C#.

Eric Lippert has been blogging about them for C# 4 though - they're
certainly considering them very carefully at the moment. Personally
I'm not entirely sure it's worth the extra complexity, but I could be
persuaded :)

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

Jon,

Gentleman's bet, the condition being whether it is going to be in there
sooner than later (with you taking sooner, and me taking later)?

=)
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
Gentleman's bet, the condition being whether it is going to be in there
sooner than later (with you taking sooner, and me taking later)?

=)

Depends on what we mean by "sooner" of course. I *hope* C# 4 is at
least a few years away - but I strongly suspect generic variance will
be in it.
 
N

Nicholas Paldino [.NET/C# MVP]

Jon,

How about, I bet it will ^not^ be in the next iteration of .NET ^or^ C#
(whichever is first)?
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
How about, I bet it will ^not^ be in the next iteration of .NET ^or^ C#
(whichever is first)?

Okay - and the wager is? A suitably humiliating signature line for a
month?
 
N

Nicholas Paldino [.NET/C# MVP]

Hmm, not what I would call a gentleman's bet, but you're on.

Remember, it's the next iteration of the framework or language,
whichever comes first.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
Hmm, not what I would call a gentleman's bet, but you're on.

Well, a gentleman's bet is (according to wikipedia):

"a bet in which no money is bet; only the honor of the two parties is
at stake"

I think a sig of "(Winner) is a superior developer and MVP in every
possible way" will do nicely on the honour side...
Remember, it's the next iteration of the framework or language,
whichever comes first.

Absolutely. My guess is that by the time it comes out, we'll both have
forgotten about this completely. Or do you have inside information that
..NET 4.0 is just around the corner? ;)
 
O

Olaf Krumnow

Thanks Jon.

I'll consider your proposal, although it's a not very elegant solution
to use a non-Generics IF to make generics work as intended...

But it doesn't seem not much of an issue to decide, that IOption<?>
would of course return an object from T result(); in lack of more
information :(

Regards
Olaf
 
N

Nicholas Paldino [.NET/C# MVP]

A sig it is. I'm going to place a reminder in outlook every six months
or so for this.

As for inside information, I don't have anything of that nature, no.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top