473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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<ColoriOpt = new Option<Color>(Color.BLACK);
IOption<FontfOpt = 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<Timplements 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
Dec 12 '07 #1
11 1520
On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.dewrote:

<snip>
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<Fooand IOption<Barare effectively
completely different types.

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

Jon
Dec 12 '07 #2
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]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:82**********************************@y5g2000h sf.googlegroups.com...
On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.dewrote:

<snip>
>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<Fooand IOption<Barare effectively
completely different types.

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

Jon

Dec 12 '07 #3
On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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
Dec 12 '07 #4
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]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:50**********************************@y5g2000h sf.googlegroups.com...
On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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

Dec 12 '07 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 12 '07 #6
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]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Dec 12 '07 #7
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 12 '07 #8
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]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Dec 12 '07 #9
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 12 '07 #10
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]" <sk...@pobox.comwrote:
On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.dewrote:

<snip>
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<Fooand IOption<Barare effectively
completely different types.

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

Jon
Dec 13 '07 #11
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]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 13 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Keith R | last post by:
Currently, generic types are not CLS compliant. This puts library authors in a quandry who are faced with three bad alternatives: 1. CLS Compliance but no generics, 2. Using generic types but...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.