I'm trying to make a struct in F# for representing depth curves in a sea map. It has to contain a list of coordinates and a float telling what depth is represented (eg. "4.5 meters"). I have a problem combining all of theese features in the struct:
- The ability to have both the list of Coordinates and the float representing depth.
- The ability to limit the list-type to be coordinates (pairs of floats) and no other type than that.
- The ability to state my Coords inside the statement creating my polygon-instanses (like this:
let myDepthCurve = {coords=[[1.;2.];[1.;2.]]};;
)
- The ability to use inline notation
The first and the second point can be solved in tandem in this slightly verbose way:
type Coord =
struct
val X : float
val Y : float
new(x,y) = { X = x ; Y = y }
end
type DepthCurve =
struct
val Coords : list<Coord>
val Depth : float
new(list_of_Coords, depth) = { Coords = list_of_Coords ; Depth = depth}
end
let myCoord1 = new Coord(1.,2.)
let myCoord2 = new Coord(3.,4.)
let myDepthCurve = new DepthCurve([myCoord1;myCoord2] , 5. )
Point three can be solved in this way
type Coord = { X : float; Y : float }
type 'a DepthCurve = {coords: 'a list;}
let myDepthCurve = {coords=[[1.;2.];[3.;4.]]};;
this brings me the short (and nice) Coord-definition but also the 'any type' string type, which I can't seem to alter to be stricly Coords. Also, I can't make the DepthCurve contain a depth-float as well, like this:
type 'a DepthCurve = {coords: 'a list; depth: float}
let myDepthCurve = {coords=[[1.;2.];[3.;4.]] , 5. };;
(this gives this error: "This expression has type 'b * 'c but is here used with type 'a list" and "no assignment given for field 'depth'")
Point 4 can be had this way:
type Coord = { X : float; Y : float }
type DepthCurve<'a> =
struct
val Coords : list <'a>
new(list_of_Coords) = { Coords = list_of_Coords }
end
let inline genDepthCurve (a: 'a list) =
new DepthCurve<'a> (a)
let myDepthCurve = genDepthCurve [1;2;3];;
but this brings the 'any type' list which I can't get rid off, and can't make coexist with my DepthCurve struct having anything else than this list.
Does anyone know how to combine them all?