Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global constants in F# - how to

Tags:

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.

like image 733
mfeingold Avatar asked Feb 24 '10 13:02

mfeingold


People also ask

What is a global constant?

Global Constants. A global constant is a literal value to which you assign a name. Like a global variable, you can access the value of the global constant from any script or 4GL procedure in the application. You set the value for the global constant when you declare it.

How do you declare a global constant in Python?

The global keyword in Python is used to modify a global variable in a local context (as explained here). This means that if the op modifies SOME_CONSTANT within myfunc the change will affect also outside the function scope (globally).

Can global functions be constant?

A constant which is needed in more than one functions can be declared a global constant by declaring it a constant using the reserve word const, initializing it and placing it outside of the body of all the functions, including the main function.

What are global Constants C++?

Global Constants in C++ C++ global constants have static linkage. This is different than C. If you try to use a global constant in C++ in multiple files you get an unresolved external error. The compiler optimizes global constants out, leaving no space reserved for the variable.


1 Answers

Use the attribute Literal:

[<Literal>]  let version = "1.2.3.4"  [<assembly:AssemblyVersion(version)>] 
like image 50
Johan Kullbom Avatar answered Oct 11 '22 14:10

Johan Kullbom