] bye = "bye" let [...">

F # Literal / constant can be composed with strings, but not int?

Why is this ok:

let [<Literal>] hi = "hi"
let [<Literal>] bye = "bye"
let [<Literal>] shortMeeting = hi + bye

      

... But it is not?

let [<Literal>] me = 1
let [<Literal>] you = 1
let [<Literal>] we = me + you

      

The third line gives an error:

This is not a valid constant expression

      

What's with that?

+3


source to share


1 answer


So the spec / docs are a little unclear, but give hints.

From the spec (for F # 3.0):

A value that has the Literal attribute is subject to the following Restrictions:

It cannot be marked modified or inline. It also may not have the ThreadStaticor ContextStatic Attributes. The right side expression must be a literal constant expression, which consists of:

Simple constant expression except (), native integer literals, unsigned native integer literals, byte array literals, BigInteger literals, and custom numeric literals.

OR

Reference to another literal

This seems to suggest that even a combination of strings is not allowed.



The documentation states that this changed in F # 3.1:

https://msdn.microsoft.com/en-us/library/dd233193.aspx

Since F # 3.1, you can use the + sign to concatenate string literals. You can also use the bitwise or (|||) operator to concatenate enumeration flags. For example, the following code is legal in F # 3.1:

Please note that integer addition is not in this list

+7


source







All Articles