

Creating Different Default Value Sets for an F# Record Type
source link: https://dev.to/robmulpeter/creating-different-default-value-sets-for-an-f-record-type-525c
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Posted on Feb 27
Creating Different Default Value Sets for an F# Record Type
The purpose of this post is to provide you with a simple working example of creating different default value sets assigned to a particular record type
in F#
. It then demonstrates how to update selected fields when binding that default to a new variable using the with
keyword.
The benefit of this approach is that the record type
remains immutable
. Record types do not support the DefaultValueAttribute
so it would mean not needing to go out of your way to create a class
with mutable
explicit fields.
Below is an example of a Meal record type
with two different value bindings, one named defaultBreakfastMeal
and the other defaultDinnerMeal
. There is nothing syntactically different between a regular binding of a record type
and one we choose to use as a default.
Record Type & Default Sets
type Meal = {
Food: string
Drink: string }
let defaultBreakfastMeal: Meal = {
Food = "Pastry"
Drink = "Coffee" }
let defaultDinnerMeal: Meal = {
Food = "Shepherd's Pie"
Drink = "Water" }
Enter fullscreen mode
Exit fullscreen mode
We can later set a new binding but update the returning default values with selected overrides. In this case, the Food
value.
let HangoverMeal = {
defaultBreakfastMeal
with
Food = "Irish Breakfast"
}
Enter fullscreen mode
Exit fullscreen mode
Output
HangoverMeal.Food = "Irish Breakfast"
HangoverMeal.Drink = "Coffee"
Enter fullscreen mode
Exit fullscreen mode
Recommend
-
10
Fusion Reactor Sets Record By Running for 20 SecondsResponses (6)
-
10
MSI OC Team sets new DDR4 overclocking world record at 7,156 MHz A new record to beat By
-
11
Tesla sets Q2 record with over 200,000 vehicles delivered Tesla delivered more vehicles in Q2 than in any previous quarter By
-
17
November 11, 2021
-
7
TechFacebook’s $232 billion fall sets record for largest one-day value drop in stock market historyPublished Thu, Feb 3 20224:28 PM ESTUpdated Fri,...
-
6
Manage DNS records and record sets by using the Azure portal Article 04/29/2021 4 minute...
-
3
Stunning 1962 Ferrari 250 GTO Berlinetta Sets $38.1m Record ...
-
7
Calculating type sets is harder than you thinkMay 16, 2022Go 1.18 added the biggest and probably one of the most requested features of all time to the language:
-
5
EU sets the deadline for smartphones to switch to USB Type-C
-
10
@noonificationNoonificationReceive Stories from @noonificationHow are you, hacker? 🪐
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK