no easy way with records in F#?
-
19-09-2019 - |
Question
In F# I can do this:
type coord = float * float //Type for a 2D-float-tupple
let myDepthCurve1 = { coords = [(1., 2.); (3., 4.)]; depth = 9.4 }
but I can't do this:
type coord = { longitude : float; latitude : float } //Type for a 2D-float-record
let myDepthCurve1 = { coords = [(longitude = 1., latitude = 2.); (longitude = 3., latitude = 4.)]; depth = 9.4 }
Is it true that I can't create my depthurve in one go when the fields in the coord type record are labeled?
Solution
You should use {}
for records, not ()
. I.e.:
type coord = { longitude : float; latitude : float } //Type for a 2D-float-record
let myDepthCurve1 = {
coords = [ { longitude = 1.; latitude = 2. };
{ longitude = 3.; latitude = 4. }];
depth = 9.4 }
OTHER TIPS
I think it would be fine if you used curly braces instead of parentheses to construct the "inline" record expression.
Assuming a type like this:
type x = { coords : coord list ; depth : float };;
You could write this:
let myDepthCurve1 = { coords = [{longitude = 1.; latitude = 2.}; {longitude = 3.; latitude = 4.}]; depth = 9.4 };;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow