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

故事扮演 提交于 2019-12-10 13:13:07

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/20779413/is-it-possible-to-use-system-type-as-static-parameter-in-f-type-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!