【问题标题】:How do access a value that belongs to a record from that record itself?如何从该记录本身访问属于该记录的值?
【发布时间】:2019-10-26 05:47:27
【问题描述】:

我真的是 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”没有定义。我猜我在这里遗漏了一些非常基本的东西,有人可以帮助我吗?我想做的事是荒谬的吗?

【问题讨论】:

    标签: f#


    【解决方案1】:

    您不能对记录执行此操作。见: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
        }
    

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多