Dhghomon / rust-fsharp

Rust - F# - Rust reference
MIT License
239 stars 14 forks source link

`with` syntax on members #5

Closed isaacabraham closed 3 years ago

isaacabraham commented 3 years ago

If you want to add methods to a record, you use with and then the member keyword, followed by this and the method you want to write. This is fairly different from Rust, but once the boilerplate is done it is quite similar.

"With" isn't needed - I'm not even sure what it does in this case! You can add members just fine without it (and, AFAIK, this is the recommended way to do things).

type Foo =
    {
        FirstName : string
        LastName : string
    }
    member this.Name = this.FirstName + " " + this.LastName

Note: I'm not sure using the declaration style you've been using will work though - most F#ers declare records as follows:

type Foo =
    { FirstName : string
      LastName : string }
    member this.Name = this.FirstName + " " + this.LastName

or as in the above sample.

Dhghomon commented 3 years ago

Oh, that looks better. I thought I found an example with "with" in the documentation yesterday but can't remember where, and since it compiled I assumed that was the norm. Will change it to that.