confusion about generics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wiktor Zychla

    #1

    confusion about generics

    I've read several documents about upcoming C# generics and I still cannot
    understand one thing.

    Would it be valid to write a code like this:

    class SomeClass
    {
    public void AMethod<T>(T a, T b)
    {
    T c = a + b; // ?
    ...
    }
    }

    since + is defined for some simple types and can be overloaded for reference
    types, then what kind of constraint should be used to allow the + in above
    context?

    or would it be forbidden to use standard binary operators in generic code?
    (in fact, it would require to have the interface, IAddable, make all numeric
    types implement the interface and then add "where T : IAddable" to the above
    code)

    if it is forbidden to use + in above context, then how will I create a
    generic class that acts like this one above? (suppose I need to call the
    method AMethod with any class that supports + operation, including <int>
    <double> and other numeric types).

    I would really like to hear from an expert on this issue.
    Thanks in advance.

    Wiktor Zychla


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: confusion about generics

    Wiktor,

    The constraint mechanism doesn't define operators on the type as a
    constraint. Because of this, you will have to get around it in some other
    way. Like you specified, an interface which indicates that the operation
    could be performed would be the best way to go.

    Generally speaking, I think that using an operation for a constraint
    would be a bad idea anyways, since not all .NET languages support operator
    overloads (VB for example) and you would limit generics on those platforms.
    However, interfaces are accessible in all languages (or at least, more
    accessible than overloaded operators).

    Because of this, you should have a constraint on an IAddable interface
    and then use the Add method on that interface (I am guessing this would be
    there).

    But, this raises the problem of what do you do for already defined
    classes which expose this functionality and can not be changed to implement
    IAddable? In this case, you will have to check the type of T, and then find
    the static method that corresponds to the operator overload (op_Addition).
    If it exists, then you can call that through Reflection. I know it is
    painful, but it is the only way with the way generics are currently
    proposed.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Wiktor Zychla" <ieUser@microso ft.com.no.spam> wrote in message
    news:e8p98ZHnDH A.1708@TK2MSFTN GP12.phx.gbl...[color=blue]
    > I've read several documents about upcoming C# generics and I still cannot
    > understand one thing.
    >
    > Would it be valid to write a code like this:
    >
    > class SomeClass
    > {
    > public void AMethod<T>(T a, T b)
    > {
    > T c = a + b; // ?
    > ...
    > }
    > }
    >
    > since + is defined for some simple types and can be overloaded for[/color]
    reference[color=blue]
    > types, then what kind of constraint should be used to allow the + in above
    > context?
    >
    > or would it be forbidden to use standard binary operators in generic code?
    > (in fact, it would require to have the interface, IAddable, make all[/color]
    numeric[color=blue]
    > types implement the interface and then add "where T : IAddable" to the[/color]
    above[color=blue]
    > code)
    >
    > if it is forbidden to use + in above context, then how will I create a
    > generic class that acts like this one above? (suppose I need to call the
    > method AMethod with any class that supports + operation, including <int>
    > <double> and other numeric types).
    >
    > I would really like to hear from an expert on this issue.
    > Thanks in advance.
    >
    > Wiktor Zychla
    >
    >[/color]


    Comment

    • Wiktor Zychla

      #3
      Re: confusion about generics

      > But, this raises the problem of what do you do for already defined[color=blue]
      > classes which expose this functionality and can not be changed to[/color]
      implement[color=blue]
      > IAddable? In this case, you will have to check the type of T, and then[/color]
      find

      this is exactly what I wanted to hear. as usual, thanks Nicholas.
      regards, Wiktor


      Comment

      Working...