In the Learning F# tutorial there are two record types with the same set of field labels.
Book record
type Book =
{ Name: string;
AuthorName: string;
Rating: int;
ISBN: string }
VHS record
type VHS =
{ Name: string;
AuthorName: string;
Rating: string; // Videos use a different rating system.
ISBN: string }
If you then create a record using these then F# will assume the last definition is the one you want to use (VHS). So under the Duplicate Label Names heading it suggests adding a type to the Rating label name like so:
let theFSharpQuizBook =
{ Name = "The F# Quiz Book";
AuthorName = "William Flash";
Book.Rating = 5;
ISBN = "1234123412" }
I would have thought that this was clearer - having the type specified in the assignment:
let theFSharpQuizBook : Book =
{ Name = "The F# Quiz Book";
AuthorName = "William Flash";
Rating = 5;
ISBN = "1234123412" }
Is there a reason that this isn't mentioned/suggested/recommended?
Children and teenagers learn by observing, listening, exploring, experimenting and asking questions. Being interested, motivated and engaged in learning is important for children once they start school. It can also help if they understand why they're learning something.
Learning by doing refers to a theory of education. This theory has been expounded by American philosopher John Dewey and Latinamerican pedagogue Paulo Freire. It's a hands-on approach to learning, meaning students must interact with their environment in order to adapt and learn.
Learning is “a process that leads to change, which occurs as a result of experience and increases the potential for improved performance and future learning” (Ambrose et al, 2010, p. 3). The change in the learner may happen at the level of knowledge, attitude or behavior.
While I can't say why it isn't mentioned in the tutorial, there are advantages and disadvantages to both. Personally, I use both, as well as a third variation, depending on circumstances.
The first option is great when you don't need to assign the record to a variable, but instead just want to use it to kick off a pipeline, like this:
{ Name = "The F# Quiz Book";
AuthorName = "William Flash";
Book.Rating = 5;
ISBN = "1234123412" }
|> Some
|> Option.toList
(Pardon the inane example, which is a really roundabout way of turning the record into a list with that single element).
On the other hand, as you say, explicitly annotating the value of the let binding may be more readable, but it requires you to use a let binding, and to give the record a name.
Here's a third option that also sometimes come in handy:
let theFSharpQuizBook'' =
{ Name = "The F# Quiz Book";
AuthorName = "William Flash";
Rating = 5;
ISBN = "1234123412" } : Book
Your thought is definitely reasonable; adding type annotation is a common technique in F#.
In this context, we could interpret the authors' suggestion as follows:
Book.Rating
is more interesting when discussing record types. It demonstrates a point that you can disambiguate different record types by using just one label name.There are some cases, using label name is more natural e.g. pattern matching:
let getRating { Book.Rating = r } = r
vs
let getRating ({ Rating = r }: Book) = r
In many cases, it's a matter of personal preference when you choose one of the two conventions for use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With