简体   繁体   English

创建自定义对象F#的列表

[英]Creating List of Custom Object F#

I have a custom object declared as follows: 我有一个自定义对象,声明如下:

class Listing : NSObject {
var from : Date?
var to : Date?
var pricePerNight : Int?
....
}

I then create an empty list of these objects like this: 然后,我创建这些对象的空列表,如下所示:

var myListings = [Listing]()

I am now trying to create the same functionality using F# . 我现在正在尝试使用F#创建相同的功能。 I have defined an interface as follows: 我定义了一个接口,如下所示:

type Listing = 

abstract member fromDate : NSDate with get,set
abstract member toDate : NSDate with get,set
abstract member pricePerNight : int with get,set
...

And then I did this: 然后我这样做了:

    let listings : List<Listing> = List.Empty

Is my understanding correct? 我的理解正确吗?

F# list (which is not the same as Swift/C# List ) is an immutable, singly linked list. F# list (与Swift / C# List )是一个不可变的单链接列表。 It is more idiomatic as a F# data structure, has better support in the language (pattern matching, etc.), and since it's immutable you can safely make it public or share it between different threads. 它作为F#数据结构更惯用,在语言中有更好的支持(模式匹配等),并且由于它是不可变的,因此您可以安全地将其公开或在不同线程之间共享。 However, it is slower than many other data structures. 但是,它比许多其他数据结构慢。

The F# equivalent of a Swift/C# List (in fact, it's exactly the same as a C# List ) is called ResizeArray , which is a mutable array of automatically variable size. Swift / C# List的F#等效项(实际上,它与C# List完全相同)称为ResizeArray ,它是一个可变大小的可变可变数组。 If you need your collection to be mutable, it's probably the data structure you want. 如果您需要使集合可变,则可能是您想要的数据结构。

Either can be the correct choice, depending on what you want to do. 取决于您要做什么,这两种方法都是正确的选择。

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

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