Tuesday, May 26, 2020

F# Records

My previous post F# - Refactoring to Functional - Part 1 garnered some attention for my use of the struct data type.

Tobias Burger pointed me at the correct implementation of F# records for my data passing. He suggested using ordinary records as my data type instead of structs since they are a more idiomatic approach to F#. It's taken me a little while to get back to the code, but I modified my data types to be records. The code is below:

type Endpoint =
{ Name: string
Url: string
Body: string
Verb: string }
let endpoint =
{ Name = "Google Test"
Url = "https://www.google.com"
Body = ""
Verb = "GET" }
view raw FsharpRecord.fs hosted with ❤ by GitHub

I have to say "Thank you" to Tobias. His criticism helped me better understand F# records, and it made my code shorter and cleaner!

No comments:

Post a Comment