Why are literals not const by default?

If F # recommends writing immutable data with a high degree of accuracy, why F # does not use a simple binding reference to constthe default?

I need to explicitly write the attributes [<Literal>]. For example:

module ConstVsStaticReadOnly =

    [<Literal>]
    let ConstInt32 = 1

    [<Literal>]
    let ConstString = "A" + "B" + "C"

    let staticReadOnlyBoolean = true
+6
source share
1 answer

When you use constin .NET, all references to it are replaced by its value at the time of compilation.

It sounds great until you understand that it is not limited to the assembly where the constant is defined. Other assemblies that reference it will also copy the constant value at compile time.

:

F #

module ConstTest 

let [<Literal>] ConstInt = 1
let NotConstInt = ConstInt

[<EntryPoint>]
let main argv = 
    printfn "%A %A" ConstTest.ConstInt ConstTest.NotConstInt
    0

1 1 - .

module ConstTest 

let [<Literal>] ConstInt = 2
let NotConstInt = ConstInt

( !), dll . 1 2. , .

, . .

+9

Source: https://habr.com/ru/post/1016734/


All Articles