views:

155

answers:

3

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?

+7  A: 

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 }
Pavel Minaev
except that it should be ; rather than , in the expression {longitude = 1., latitude = 2.}Is there any systematic pattern in the usage of , and ; in F#?
loldrup
Fixed. The pattern is simple - comma is a tuple-producing operator (note that it doesn't require parentheses), so everything else has to use semicolon to disambiguate.
Pavel Minaev
+1  A: 

I think it would be fine if you used curly braces instead of parentheses to construct the "inline" record expression.

harms
+1  A: 

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 };;

Tim Stewart
Thanks guys. Small detail, big difference.
loldrup