Generic constraint: Enforce type to have static function and constructor with parameters
Asked Answered
A

1

5

I know you can write:

class GenericClass<T> where T : new()
{ 

}

to enforce that T has an empty constructor.

My Qs are :

  1. can you enforce that T has a constructor with a specific type of parameter? Like:

    class SingletonFactoryWithEmptyConstructor<T> where T : new(int)
    
  2. can you enforce that T has a static function (let's say, void F()) so that you can use this function inside the generic class? Like :

    class GenericClass<T> where T : void F()
    { 
       void G ()
       {
           T.F();
       }
    }
    

    I know you can specify that T implements an interface but I don't want that. I want to specify that T has a static function.

Antibody answered 7/5, 2011 at 7:46 Comment(1)
M
6

No, there's nothing like this in C#.

I've previously suggested that "static interfaces" could express this reasonably neatly. They'd only be useful for generic type constraints (I suspect, anyway) but then you could express:

  • Constructors with arbitrary parameters
  • Static methods and properties
  • Operators

The last of these points is particularly interesting in my view, allowing things like a generic "Average" method over numeric types with suitable addition and division operators.

I believe some folks at MS have thought about something similar, but I haven't heard anything to suggest they're actively working on it.

Miche answered 7/5, 2011 at 7:54 Comment(8)
I shudder whenever I think of the kludges I've had to assemble to get around this limitation in the past.. (I deal a lot with generics). How I long for a static interface, nice solution to the problem.Lilliamlillian
Apparently this is a limitation of C# not of the CLR. F#, for example, allows to impose "explicit member constraints" like a presence of a method or property on a type...Stepdaughter
@Paul: I'd be interested to see how that translates to IL... and does it include static members?Miche
@Jon So'd be I... Haven't tried yet myself. Spin or cut and paste some code (e.g. from codebetter.com/matthewpodwysocki/2009/09/28/…, he's citing you very frequently) and have a look at IL. And yes, static members are explicitely used in all the examples. I assume these features are not done by some sort of F# compiler hackery, it really seems to be supported by the CLI. Interestingly, explicit member constraints were marked as "not intended for common use" on MSDN until yesterday. Today, this restriction is gone.Stepdaughter
@Paul, @Jon: I don't believe "static interfaces" (explicit member constraints, as they're known in F# - aka, duck typing) have anything to do with the CLR. It's purely a feature of the F# compiler, which is easily demonstrated by the fact that functions using such constraints must be marked inline.Dodona
@Jon: Yes, F#'s explicit member constraints support static members (and operators).Dodona
After a first (a bit more more than brief) reading on "Statically resolved type parameters (F#)" now I also believe that this is a feature of the F# compiler... Need to read up more on that though...Stepdaughter
I have tried to capture the current state of play here: https://mcmap.net/q/521848/-restricting-a-generic-type-parameters-to-have-a-specific-constructorCommodus

© 2022 - 2024 — McMap. All rights reserved.