Creating generic types through type providers

Is it possible to create a generic type through type providers so that

[<TypeProvider>] type SampleTypeProvider(config: TypeProviderConfig) as this = ... //the below would be the generated type type A<'b> () = member this.C() : 'b = ... member this.D() : 'b = ... // ... [<assembly:TypeProviderAssembly>] do() .... 

so something will look like in the use case

 #r @".\bin\Debug\SampleTypeProvider.dll" type A = SampleTypeProvider.A type intA = A<int> type strA = A<str> 

And if it is possible - how can I approach him.

+5
source share
1 answer

This is not possible using standard methods. I tried to look back, but could not find the canonical link, but this is a known limitation, and there were various proposals to remove the limitation.

Ross McKinlay has a somewhat extreme project provider such as Mixin , which works around this, actually creating an F # source code file when the provider type (and you can include this file in your project). This may be more code generation than a type provider, but talking about the topic is also a good explanation of some limitations.

How to solve this very much depends on the type of supplier’s goal. If you need only a limited number of types, you can use something like static parameters and write A<"int"> or A<"string"> . You can also mix regular non-generic generic types with non-generic types (in a sense). But I think you need to write more about your specific use case in order to get a better answer.

+4
source

Source: https://habr.com/ru/post/1258836/


All Articles