Type provider and static argument in F#

六眼飞鱼酱① 提交于 2019-12-07 14:48:01

问题


I have a library, Library_1, which compiles correctly and defines a provided type :

type modelforexcel =  FSharpx.ExcelFile<@"template.xls", "Brokernet", true>

When I include this library in another project, Library_2, the compiler complains that it can't find any "Brokernet.template.xls", at the root of the new Library_2 project.

Error   4   'C:\Library_2\template.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.

I would like the type to refer to the original "Brokernet.template.xls", so I am trying to provide the complete path to it, but

type modelforexcel =  
   FSharpx.ExcelFile<__SOURCE_DIRECTORY__+@"Brokernet.template.xls", "Brokernet", true>

does not work, as I guess it is not a literal (?) But 'obviously' defining this literal does not work either

[<Literal>]
let a = __SOURCE_DIRECTORY__+@"Brokernet.template.xls"

Are there any way to define such a 'dynamic literal' ?

edit

Interestingly enough, if I define my type inside a module in the first library

module Load =
   [<Literal>]
   let a = @"Brokernet.template.xls"
   type modelforexcel =  FSharpx.ExcelFile< a , "Brokernet", true>

Then the type is not 'regenerated' upon using the first library in the second one, and the type provider does not complain about the file being absent of the root of the 2nd library.

This sheds profound insights in the compilation model of F# that are probably best exposed by masters. I'd just say, as a rough man, that "the code is in modules"

PS : I guess thats one more problem which would be solved by a proper staged compilation.


回答1:


As being shown in the comments, using relative paths e.g. @"..\Library_1\Brokernet.template.xls" solved the problem.



来源:https://stackoverflow.com/questions/11725326/type-provider-and-static-argument-in-f

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