繁体   English   中英

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

[英]Can I create own literal types on .NET (F#)

例如,如果我想要Nullable(x)的文字类型,如a

let x = 6x // nullable literal type 

那么制作自己的文字类型是真的吗?

是的你可以

F#规格:

带有后缀Q,R,Z,I,N,G的整数文字通过以下语法翻译用于用户定义和库定义的类型: xxxx <suffix>

  • 对于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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM