gpt4 book ai didi

F# 文字/常量可以由字符串组成,但不能由 int 组成?

转载 作者:行者123 更新时间:2023-12-04 23:19:41 25 4
gpt4 key购买 nike

为什么可以:

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

……但这不是吗?
let [<Literal>] me = 1
let [<Literal>] you = 1
let [<Literal>] we = me + you

第三行给出了错误:
This is not a valid constant expression

那是怎么回事?

最佳答案

所以规范/文档有点不清楚,但提供了提示。

从规范(对于 F# 3.0):

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

It may not be marked mutable or inline. It may not also have the ThreadStaticor ContextStatic attributes. The righthand side expression must be a literal constant expression that is made up of either:

A simple constant expression, with the exception of (), native integer literals, unsigned native integer literals, byte array literals, BigInteger literals, and user-defined numeric literals.

OR

A reference to another literal



这似乎表明,即使是字符串的组合也是不允许的。

文档指出,这在 F# 3.1 中发生了变化:

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

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



请注意,整数加法不在该列表中

关于F# 文字/常量可以由字符串组成,但不能由 int 组成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com