How do access a value that belongs to a record from that record itself?

Viewed 57

I am REALLY new to F#, so I might have used the wrong terminology here. Please feel free to correct me if I am wrong, I would really appreciate it! Anyways, on to the question

I have a record that I have defined as so:

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

And I have created an instance of that record like so:

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")})
    } 

On the line where I am trying to assign a value the Image member, It tells me that the value or constructor 'Address' is not defined.

I have tried using a self-identifier on the instantiation of the record and then trying to access Address like

this.Address

but it tells me that 'this' is not defined. I am guessing I am missing something quite fundamental here, can anyone help me? Is what I am trying to do nonsensical?

1 Answers

You can't do this with records. See: Reference a record from within itself during construction

You can do it with another binding (I couldn't get your code to compile and have simplified it):

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


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