TL; DR: F#コンパイラはdetermined by Eugene Fotinとexpanded upon by Gene Belitskiとして、int
operatorこの文脈でint
を解釈します。最も良い回避策は、System.Int32.MaxValue
または以下に説明するような一意のエイリアスを使用することです。F#int.MaxValueは「有効な定数式ではありませんが、System.Int32.MaxValueは?
次のレコードタイプ考えてみましょう:
type User = {
Username : string
}
を私はUsername
は3文字以上の長になりたいので、私はStringLength
属性を使用します。そこには、最大長はありませんので、私はint.MaxValue
に設定します。
type User = {
[<StringLength(int.MaxValue, MinimumLength=3)>]
Username : string
}
これは私に次のエラーを与える:私は代わりにSystem.Int32
を使用する場合
This is not a valid constant expression or custom attribute value.
すべてが桃です:
type User = {
[<StringLength(System.Int32.MaxValue, MinimumLength=3)>]
Username : string
}
別名int
の場合はコンパイルされます。
type User = {
[<StringLength(num.MaxValue, MinimumLength=3)>]
Username : string
}
and num = int
または完全にタイプを修飾:
type User = {
[<StringLength(Microsoft.FSharp.Core.int.MaxValue, MinimumLength=3)>]
Username : string
}
私はF#のソースとint
is defined exactly as you would expectでチェック:
type int32 = System.Int32
// Then, a few lines later…
type int = int32
何が起こっていますか?私は、F#プリミティブ型は、ほとんどのコンテキストで他の型と互換性があると仮定しましたが、私のメンタルモデルから何かが欠落しているように見えます。
あなたはそうです、それは起こっていることです。それはなぜですか? F#文法の何か、私は思いますか? –