Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I provide a Type as an input to a Type provider in F#?

Are there any pitfall I should be aware of doing so ?

Would you know of existing code dealing with the same pb I might encounter ?

Thks

like image 868
nicolas Avatar asked Mar 03 '12 15:03

nicolas


1 Answers

Unfortunately, you cannot pass types as static parameters to a type provider. The static parameters passed using MyProvider< "first argument", 42 > have to be primitive types (like string, int and similar). I don't see the list anywhere in the documentation, but Type is definitely not supported.

The problem with doing this is that you could pass it typeof<MyType> where MyType is declared in the same file as the file that's using the type provider, and so the F# compiler would have to first compile the first part of a file, then invoke the provider and then continue. This sounds possible, but it is probably low-priority for the F# team.

The best way to pass information about type to a type provider is to give it a type name and then lookup the type using reflection (this will only work if the type is from an already compiled assembly):

type MyTest = MyTypeProvider<"System.Int32">
like image 147
Tomas Petricek Avatar answered Nov 08 '22 14:11

Tomas Petricek