Simple (?) generics question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olaf Krumnow

    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<ColoriO pt = new Option<Color>(C olor.BLACK);
    IOption<FontfOp t = new Option<Font>(Fo nt.getFont("xyz "));
    List<IOption<?> list = new ArrayList<IOpti on<?>>();
    list.add(iOpt);
    list.add(fOpt);
    for (IOption<?optio n : list) {
    System.out.prin tln(option.resu lt().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<Timpleme nts 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
  • Jon Skeet [C# MVP]

    #2
    Re: Simple (?) generics question

    On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.d ewrote:

    <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

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      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.co m

      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:82b315c2-f49c-48dd-bc10-d2c9daa04168@y5 g2000hsf.google groups.com...
      On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.d ewrote:
      >
      <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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Simple (?) generics question

        On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
        <m...@spam.guar d.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

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          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.co m

          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:50376abb-9ce9-4456-a24e-d49ee7c22be6@y5 g2000hsf.google groups.com...
          On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
          <m...@spam.guar d.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

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Simple (?) generics question

            Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
            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 - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            World class .NET training in the UK: http://iterativetraining.co.uk

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              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.co m

              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:MPG.21ca2c 5a24352bed6e9@m snews.microsoft .com...
              Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
              > 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 - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              World class .NET training in the UK: http://iterativetraining.co.uk

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Simple (?) generics question

                Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
                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 - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                World class .NET training in the UK: http://iterativetraining.co.uk

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  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.co m

                  "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  news:MPG.21ca45 dd1109db966ec@m snews.microsoft .com...
                  Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
                  > 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 - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  World class .NET training in the UK: http://iterativetraining.co.uk

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Simple (?) generics question

                    Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
                    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 - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    World class .NET training in the UK: http://iterativetraining.co.uk

                    Comment

                    • Olaf Krumnow

                      #11
                      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]" <sk...@pobox.co mwrote:
                      On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.d ewrote:
                      >
                      <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

                      Comment

                      • Nicholas Paldino [.NET/C# MVP]

                        #12
                        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.co m

                        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                        news:MPG.21ca6f e6367a57d6ed@ms news.microsoft. com...
                        Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
                        > 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 - <skeet@pobox.co m>
                        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                        World class .NET training in the UK: http://iterativetraining.co.uk

                        Comment

                        Working...