Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use the Literal attribute in an F# Signature file (.fsi)

Tags:

f#

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.

like image 361
Troy Kershaw Avatar asked Jan 11 '18 17:01

Troy Kershaw


People also ask

What are the literal types in F #?

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

What does can’t assign to literal mean in Python?

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:

Are the literal types in F #Case-Sensitive?

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.


1 Answers

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.

like image 55
Abel Avatar answered Nov 06 '22 04:11

Abel