Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set record property when creating instance

Tags:

f#

I have a record type

type Record = { Start: DateTime; End: DateTime; Duration: TimeSpan }
let record = { Start = DateTime.Now; End = DateTime(2021,12,1) }

This code won't compile as there's no assignment given for field Duration.

Is it possible to calculate Duration in the type definition instead of assigning it?

like image 495
Alexander Zeitler Avatar asked Jun 15 '26 18:06

Alexander Zeitler


1 Answers

Solved it:

type Record = { Start: DateTime; End: DateTime; }
with
  member this.Duration = this.End - this.Start

let record = { Start = DateTime.Now; End = DateTime(2021,12,1) }
like image 105
Alexander Zeitler Avatar answered Jun 17 '26 23:06

Alexander Zeitler