 |
|
|
|
| Author |
Message |
Olaf Krumnow
Joined: 12 Dec 2007 Posts: 2
|
Posted: Wed Dec 12, 2007 11:30 am Post subject: Simple (?) generics question |
|
|
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 iOpt = new Option(Color.BLACK);
IOption fOpt = new Option(Font.getFont("xyz"));
List> list = new ArrayList>();
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 result();
}
public class Option implements IOption {
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
Archived from group: microsoft>public>dotnet>languages>csharp |
|
| Back to top |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 27
|
Posted: Wed Dec 12, 2007 12:24 pm Post subject: Re: Simple (?) generics question |
|
|
On Dec 12, 2:30 pm, Olaf Krumnow wrote:
> 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 and IOption are effectively
completely different types.
What you *could* do is make IOption also implement a non-generic
IOption interface, and create a List.
Jon |
|
| Back to top |
|
 |
Nicholas Paldino [.NET/C#
Joined: 08 Aug 2007 Posts: 71
|
Posted: Wed Dec 12, 2007 4:08 pm Post subject: Re: Simple (?) generics question |
|
|
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" wrote in message @y5g2000hsf.googlegroups.com...
> On Dec 12, 2:30 pm, Olaf Krumnow wrote:
>
>
>
>> 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 and IOption are effectively
> completely different types.
>
> What you *could* do is make IOption also implement a non-generic
> IOption interface, and create a List.
>
> Jon |
|
| Back to top |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 27
|
Posted: Wed Dec 12, 2007 1:21 pm Post subject: Re: Simple (?) generics question |
|
|
On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
wrote:
> 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 |
|
| Back to top |
|
 |
Nicholas Paldino [.NET/C#
Joined: 08 Aug 2007 Posts: 71
|
Posted: Wed Dec 12, 2007 5:14 pm Post subject: Re: Simple (?) generics question |
|
|
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)?
=)
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" wrote in message @y5g2000hsf.googlegroups.com...
> On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
> wrote:
>> 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 |
|
| Back to top |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 266
|
Posted: Wed Dec 12, 2007 10:36 pm Post subject: Re: Simple (?) generics question |
|
|
Nicholas Paldino [.NET/C# MVP] wrote:
> 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.
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk |
|
| Back to top |
|
 |
Nicholas Paldino [.NET/C#
Joined: 08 Aug 2007 Posts: 71
|
Posted: Wed Dec 12, 2007 6:41 pm Post subject: Re: Simple (?) generics question |
|
|
Jon,
How about, I bet it will ^not^ be in the next iteration of .NET ^or^ C#
(whichever is first)?
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" wrote in message @msnews.microsoft.com...
> Nicholas Paldino [.NET/C# MVP] wrote:
>> 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.
>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk |
|
| Back to top |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 266
|
Posted: Thu Dec 13, 2007 12:54 am Post subject: Re: Simple (?) generics question |
|
|
Nicholas Paldino [.NET/C# MVP] wrote:
> 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?
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk |
|
| Back to top |
|
 |
Nicholas Paldino [.NET/C#
Joined: 08 Aug 2007 Posts: 71
|
Posted: Wed Dec 12, 2007 8:47 pm Post subject: Re: Simple (?) generics question |
|
|
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" wrote in message @msnews.microsoft.com...
> Nicholas Paldino [.NET/C# MVP] wrote:
>> 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?
>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk |
|
| Back to top |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 266
|
Posted: Thu Dec 13, 2007 3:24 am Post subject: Re: Simple (?) generics question |
|
|
Nicholas Paldino [.NET/C# MVP] wrote:
> 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?
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk |
|
| Back to top |
|
 |
Olaf Krumnow
Joined: 12 Dec 2007 Posts: 2
|
Posted: Thu Dec 13, 2007 4:50 am Post subject: Re: Simple (?) generics question |
|
|
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
On 12 Dez., 16:24, "Jon Skeet [C# MVP]" wrote:
> On Dec 12, 2:30 pm, Olaf Krumnow wrote:
>
>
>
> > 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 and IOption are effectively
> completely different types.
>
> What you *could* do is make IOption also implement a non-generic
> IOption interface, and create a List.
>
> Jon |
|
| Back to top |
|
 |
Nicholas Paldino [.NET/C#
Joined: 08 Aug 2007 Posts: 71
|
Posted: Thu Dec 13, 2007 3:38 pm Post subject: Re: Simple (?) generics question |
|
|
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" wrote in message @msnews.microsoft.com...
> Nicholas Paldino [.NET/C# MVP] wrote:
>> 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?
>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
|
|
| Back to top |
|
 |
|
|
| Related Topics: | Simple Question Hi, I am writing a graphing application that needs to have multiple instances of custom graphs written vertically. I then need a governer that can interact with all the graphs by drawing a vertical line to line up the graph points. My basic question is I
Simple question Why is dotnet called so? I mean, several peoples mix dotnet development with domain names ... And several peoples also think that with dotnet framework, you can only develop internet based Where do this came from? An
Newbe Question: How to get a simple class to display in Auto Hi, sorry if this is off group but I'm new to this and don't know where to start. I've designed several classes and I want them to display succinctly in the window. For example I have a point class and I would like it to show up like Poin
How to use generics with reflection? Hi, I have a runtime generated class (a WCF proxy) that extends a generic type. The generated code is: "3.0.0.0")] public class _MyTypeClient : _MyType { ..... } Now I would l
"Cyclic reference" with generics I need a factory that produces items, and the items need to know about their factory. Something like: class Factory { Item GetNewItem(); } class Item { Factory GetFactory(); } Now, I would like to add some generics to the catory: class wher |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|