I'm unable to define a literal in an F# signature file.
I have a file, File.fs
, as below:
module Thingo =
[<Literal>]
let hello = "world"
I need to create a signature file for it, so initially tried this (File.fsi
):
module Thingo =
[<Literal>]
val hello : string
This caused the following error in File.fsi
: A declaration may only be the [<Literal>] attribute if a constant value is also given, e.g. 'val x : int = 1'
I gave that a go:
module Thingo =
[<Literal>]
val hello : string = "world"
The line after the val hello
declaration received the error: Incomplete structured construct at or before this point in signature file. Expected incomplete structured construct at or before this point or other token.
So it doesn't like me assigning values in a signature file (or I don't know how to assign the value, very much a possibility).
The docs say that I do need to assign the value in the signature somehow:
If the
Literal
attribute is used, it must appear in both the signature and the implementation, and the same literal value must be used for both.
(from https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/signatures)
I also auto-generated a signature file (with the --sig
build argument) and it spat out:
module EventSchema = begin
[<LiteralAttribute ()>]
val id : string
end
I tried this exact syntax, but it didn't work either.
Thanks for reading! Any suggestions will be greatly appreciated.
The following table shows the literal types in F#. Characters that represent digits in hexadecimal notation are not case-sensitive; characters that identify the type are case-sensitive. Type Description Suffix or prefix Examples sbyte signed 8-bit integer y 86y0b00000101y byte unsigned 8-bit natural number uy 86uy0b00000101uy int16
SyntaxError: can’t assign to literal A variable is a unique identifier for a value. When you reference a variable, Python will read the value associated with that variable. Python uses an equals sign to assign values to a variable:
The following table shows the literal types in F#. Characters that represent digits in hexadecimal notation are not case-sensitive; characters that identify the type are case-sensitive.
The correct answer should have been: it's a bug. It was reported here: https://github.com/dotnet/fsharp/issues/7897.
Note that, as a workaround, you can switch light syntax off, then it will work in current versions.
Note also that the compiler will error when the constant values don't match with the one in the signature file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With