F# 嵌套记录
F# nested records
- 嵌套记录
记录是否类似于字典,它是一棵具有名称的对象树?
或者记录只是简单类型的列表
let r = { b = { a = 2 } } // is this possible? if not, how to achieve? is this useful in f#?
对于我来说,有歧视的工会可以实现类似的行为(下面的代码)
// Discriminated union for a card's suit
type Suit = | Heart | Diamond | Spade | Club
let suits = [Heart; Diamond; Spade; Club]
suits
// Discriminated union for playing cards
type PlayingCard =
| Ace of Suit
| King of Suit
| Queen of Suit
| Jack of Suit
| ValueCard of int * Suit
// generating a deck of cards
let deckOfCards =
[
for suit in [Spade; Club; Heart; Diamond] do
yield Ace(suit)
yield King(suit)
yield Queen(suit)
yield Jack(suit)
for value in 2..10 do
yield ValueCard(value, suit)
]
它有点类似于 python 或 idk 中的字典。下面的代码是虚拟的
type Customer =
{
FirstName : string
Contacts =
{
WorkPhone : string
MobilePhone : string
}
}
可以使用 anonymous records:
创建嵌套类型
type Customer =
{
FirstName : string
Contacts :
{|
WorkPhone : string
MobilePhone : string
|}
}
let customer =
{
FirstName = "John"
Contacts =
{|
WorkPhone = "123-456-7890"
MobilePhone = "234-567-8901"
|}
}
是的,您可以有嵌套记录,但就像在您的示例中使用可区分联合一样,您需要为嵌套类型命名:
type CustomerConracts =
{
WorkPhone : string
MobilePhone : string
}
type Customer =
{
FirstName : string
Contacts: CustomerConracts
}
let c = { FirstName = "John", Contacts = { WorkPhone = "123", Mobile phone = "098" } }
您可以在代码中看到一些模式,但记录与字典不相似。您可以将它们视为 类 而具有强类型 public 属性。如果需要创建字典,则必须使用可用的 map/dictionary 类 之一或实现自己的字典。例如,看一下 Map 类型。
https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-fsharpmap-2.html
type Contact =
{
WorkPhone : string
MobilePhone : string
}
type Customer =
{
FirstName : string
Contacts : Contact
}
let cust : Customer =
{
FirstName = "Joe"
Contacts = { WorkPhone="1800131313"; MobilePhone="0863331311" }
}
上面的代码表明您可以嵌套记录类型。除了按照@brianberns 的建议使用匿名记录外,您还可以声明您计划嵌套的数据类型。
- 嵌套记录
记录是否类似于字典,它是一棵具有名称的对象树? 或者记录只是简单类型的列表
let r = { b = { a = 2 } } // is this possible? if not, how to achieve? is this useful in f#?
对于我来说,有歧视的工会可以实现类似的行为(下面的代码)
// Discriminated union for a card's suit
type Suit = | Heart | Diamond | Spade | Club
let suits = [Heart; Diamond; Spade; Club]
suits
// Discriminated union for playing cards
type PlayingCard =
| Ace of Suit
| King of Suit
| Queen of Suit
| Jack of Suit
| ValueCard of int * Suit
// generating a deck of cards
let deckOfCards =
[
for suit in [Spade; Club; Heart; Diamond] do
yield Ace(suit)
yield King(suit)
yield Queen(suit)
yield Jack(suit)
for value in 2..10 do
yield ValueCard(value, suit)
]
它有点类似于 python 或 idk 中的字典。下面的代码是虚拟的
type Customer =
{
FirstName : string
Contacts =
{
WorkPhone : string
MobilePhone : string
}
}
可以使用 anonymous records:
创建嵌套类型type Customer =
{
FirstName : string
Contacts :
{|
WorkPhone : string
MobilePhone : string
|}
}
let customer =
{
FirstName = "John"
Contacts =
{|
WorkPhone = "123-456-7890"
MobilePhone = "234-567-8901"
|}
}
是的,您可以有嵌套记录,但就像在您的示例中使用可区分联合一样,您需要为嵌套类型命名:
type CustomerConracts =
{
WorkPhone : string
MobilePhone : string
}
type Customer =
{
FirstName : string
Contacts: CustomerConracts
}
let c = { FirstName = "John", Contacts = { WorkPhone = "123", Mobile phone = "098" } }
您可以在代码中看到一些模式,但记录与字典不相似。您可以将它们视为 类 而具有强类型 public 属性。如果需要创建字典,则必须使用可用的 map/dictionary 类 之一或实现自己的字典。例如,看一下 Map 类型。 https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-fsharpmap-2.html
type Contact =
{
WorkPhone : string
MobilePhone : string
}
type Customer =
{
FirstName : string
Contacts : Contact
}
let cust : Customer =
{
FirstName = "Joe"
Contacts = { WorkPhone="1800131313"; MobilePhone="0863331311" }
}
上面的代码表明您可以嵌套记录类型。除了按照@brianberns 的建议使用匿名记录外,您还可以声明您计划嵌套的数据类型。