📅  最后修改于: 2021-01-01 14:28:12             🧑  作者: Mango
F#结构是用于组织数据的数据结构。它是值类型,比类有效。它不允许let绑定。您必须使用val关键字声明字段。它允许定义字段,但不允许初始化。您可以在结构中创建构造函数,可变字段和不可变字段。
句法:
[ attributes ]
type [accessibility-modifier] type-name =
struct
type-definition-elements
end
// or
[ attributes ]
[]
type [accessibility-modifier] type-name =
type-definition-elements
在这里,我们创建了一个Book结构,其中包含书的详细信息。
type Book = struct
val title : string
val author: string
val price : float
new (title, author, price) = // Constructor
{title = title; author = author; price = price;}
end
let book = new Book("FSharp Programming", "Chris Smith", 100.0) // Calling custructor
printfn "-------------------------Book's Details-------------------------"
printfn "Title : %s " book.title
printfn "Author : %s" book.author
printfn "Price : $%0.2f" book.price
输出:
-------------------------Book's Details-------------------------
Title : FSharp Programming
Author : Chris Smith
Price : $100.00