如何从记录本身访问属于记录的值?

时间:2019-06-11 18:38:28

标签: f#

我真的是F#的新手,所以我在这里可能使用了错误的术语。如果我错了,请随时纠正我,我将不胜感激!无论如何,还是要问这个问题

我有一条定义如下的记录:

    type EventSource = {
        SourceName: string
        Address:    string 
        ParseDocument: HtmlDocument -> Event seq }

我已经创建了该记录的实例,如下所示:

let lofSource = {
    SourceName = "LOF"
    Address = "https://lof.dk/syd/kurser"
    ParseDocument = fun document ->
        document.Descendants ["div"]
        |> Seq.filter (fun d -> d.HasClass("item"))
        |> Seq.map (
            fun e ->
            let linkElement 
                = e.Descendants (fun j -> j.HasClass "item-title") 
                |> Seq.head 
                |> (fun y -> y.Descendants ["a"]) 
                |> Seq.map (fun fa -> fa.Attribute "href") 
                |> Seq.head
            {         
                Title = e.AttributeValue "data-holdnavn"  
                Link =  linkElement.Value() 
                Status = e.AttributeValue "data-status"
                Image = Address //Here! 
                City = e.AttributeValue "data-bynavn"
                Date = System.DateTime.ParseExact(e.AttributeValue("data-datosort"), "yyyyMMdd", null);
                PostalCode = e.AttributeValue("data-postnr")})
    } 

在我尝试为Image成员分配值的那一行,它告诉我未定义值或构造函数“地址”。

我尝试在记录的实例化上使用自我标识符,然后尝试访问地址,例如

this.Address

但是它告诉我“ this”没有定义。我猜我在这里缺少了一些基本的东西,有人可以帮助我吗?我想做的是荒谬的吗?

1 个答案:

答案 0 :(得分:3)

您不能使用记录执行此操作。参见:Reference a record from within itself during construction

您可以使用另一个绑定来完成此操作(我无法编译您的代码并对其进行了简化):

type EventSource = {
   SourceName: string
   Address:    string 
   ParseDocument: string -> string}


let lofSource = 
    let helloThere = "General Kenobi"
    {        
        SourceName = "LOF"
        Address = foo
        ParseDocument = fun document ->                      
            foo
    }