Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use System.Type as static parameter in F# type provider?

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.

like image 291
Xiang Zhang Avatar asked Dec 26 '13 05:12

Xiang Zhang


Video Answer


1 Answers

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>
like image 159
Tomas Petricek Avatar answered Oct 21 '22 14:10

Tomas Petricek