F#中的类型提供程序和静态参数 [英] Type provider and static argument in F#

查看:84
本文介绍了F#中的类型提供程序和静态参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库Library_1,可以正确编译并定义提供的类型:

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

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

当我将此库包含在另一个项目Library_2中时,编译器会抱怨在 new Library_2项目的根目录中找不到任何"Brokernet.template.xls".

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.

我希望该类型引用原始的"Brokernet.template.xls",因此我尝试提供其完整路径,但是

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' ?

修改

有趣的是,如果我在第一个库中的模块中定义了我的类型

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.

这为F#的编译模型提供了深刻的见解,而这些见解可能是最有经验的.粗略地说,我只是说代码在模块中"

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:我想这是一个另外的问题,可以通过适当的分阶段编译来解决.

推荐答案

如注释中所示,使用相对路径,例如@"..\Library_1\Brokernet.template.xls"解决了这个问题.

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

这篇关于F#中的类型提供程序和静态参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆