Question

I was wondering is it possible to use System.Type as the static parameter in F# type provider, so that I can write something like:

type HelperType = HelperProvider<typeof<int>>

The idea is, is it possible to let type provider generating some helper type based on some .NET type.

Was it helpful?

Solution

No, type provider parameters can only be of primitive types (like int and string). The best you can do is to take the type name as a string:

type HelperType = HelperProvider<"int">

This will do the trick for primitive (and standard types), but it won't work for types that are defined earlier in the file (or in the project) where you're using the type provider.

As far as I know, this is definitely something that the F# team has been looking into - it would allow some interesting meta-programming applications. The main focus for now has been on data access, so this has not been such a priority (out o curiosity, what application do you have in mind?)

By the way - passing types as parameters can cause some interesting tricky questions. For example, how would the compiler handle something like this:

type A = MyProvider<B>
and B = MyProvider<A>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top