我可以在.NET(F#)上创建自己的文字类型吗

| 例如,如果我想要Nullable(x)的文字类型,例如
let x = 6x // nullable literal type 
那么创建自己的文字类型是否真实?     
已邀请:
是的你可以 F#规范: 通过以下语法转换将后缀为Q,R,Z,I,N,G的整数文字用于用户定义和库定义的类型: xxxx <后缀> 对于xxxx = 0-NumericLiteral.FromZero() 对于xxxx = 1-NumericLiteral.FromOne() 对于Int32范围内的xxxx-NumericLiteral.FromInt32(xxxx) 对于Int64范围内的xxxx-NumericLiteral.FromInt64(xxxx) 对于其他数字-NumericLiteral.FromString(\“ xxxx \”) 例如,如下定义模块NumericLiteralZ允许使用文字形式32Z来生成32个'Z'字符的序列。没有文字语法可用于32位整数范围以外的数字。
module NumericLiteralZ =
    let FromZero() = \"\"
    let FromOne() = \"Z\"
    let FromInt32 n = String.replicate n \"Z\"

// nullables
open System
module NumericLiteralN = 
    let FromZero()          = Nullable(0)
    let FromOne()           = Nullable(1)
    let FromInt32(i : int)  = Nullable(i)

printfn \"%A\" 0N
printfn \"%A\" 1N
printfn \"%A\" 100N.HasValue
    

要回复问题请先登录注册