Among several other state-related types, I have the following types of records in my code:
type SubmittedSuggestionData = { SuggestionId : Guid SuggestionText : string Originator : User ParentCategory : Category SubmissionDate : DateTime } type ApprovedSuggestionData = { SuggestionId : Guid SuggestionText : string Originator : User ParentCategory : Category SubmissionDate : DateTime ApprovalDate : DateTime }
Then they are passed to the following:
type Suggestion = | SubmittedSuggestion of SubmittedSuggestionData | ApprovedSuggestion of ApprovedSuggestionData
This gives me the opportunity to work with the State machine style template to execute specific state-specific business logic. (This approach was taken from: http://fsharpforfunandprofit.com/posts/designing-with-types-representing-states/ )
I have a function that in its simplest form changes a SubmittedSuggestion to ApprovedSuggestion :
let ApproveSuggestion suggestion = match suggestion with | SubmittedSuggestion suggestion -> ApprovedSuggestion {}
This function is not complete at the moment, because what I'm trying to understand is when the sentence changes from "Presented in Approved", how do you copy the properties from the passed in suggestion to the newly created ApprovedSuggestion , as well as filling out the new ApprovalDate property?
I think this will work if I do something like:
let ApproveSuggestion suggestion = match suggestion with | SubmittedSuggestion {SuggestionId = suggestionId; SuggestionText = suggestionText; Originator = originator; ParentCategory = category; SubmissionDate = submissionDate} -> ApprovedSuggestion {SuggestionId = suggestionId; SuggestionText = suggestionText; Originator = originator; ParentCategory = category; SubmissionDate = submissionDate; ApprovalDate = DateTime.UtcNow}
but it looks pretty terrible to me.
Is there a cleaner, more concise way to get the same result? I tried using the with keyword, but it did not compile.
thanks
source share