Global constants in F# - how to

心不动则不痛 提交于 2019-12-04 16:39:40

问题


I need to set a version number to be used in the AssemblyVersion attribute by several related projects.

In C# I use the following

public class Constants {
    public const string Version = "1.2.3.4";
}

then it can be used as follows:

[assembly:AssemblyVersion(Constants.Version)]

What would be the equivalent construct in F#. All my attempts to come up with a binding which can be accepted as an attribute argument did not work.


回答1:


Use the attribute Literal:

[<Literal>] 
let version = "1.2.3.4"

[<assembly:AssemblyVersion(version)>]



回答2:


Since I stepped into this trap myself I thought I'd share for anyone following. A 'Literal' requires that the letter starts with a capital letter. This will hit you when you try to use the literal in a pattern matching construct.

Reference: Literal attribute not working



来源:https://stackoverflow.com/questions/2326398/global-constants-in-f-how-to

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