Thursday 15 January 2015

f# - Generic class with a measure constraint -



f# - Generic class with a measure constraint -

how create generic class in f#, constraint type measure?

i've tried a2 , b2 not produce errors:

open microsoft.fsharp.data.unitsystems.si.unitnames type vector2d_a<[<measure>] 'u>(x : float<'u>, y : float<'u>) = fellow member this.x = x fellow member this.y = y type vector2d_b<'t, [<measure>] 'u>(x : 't, y : 't) = fellow member this.x = x fellow member this.y = y type vector2d_c<'t>(x : 't, y : 't) = fellow member this.x = x fellow member this.y = y allow a1 = vector2d_a(1.0<metre>, 2.0<metre>) allow b1 = vector2d_a(1.0<metre>, 2.0<metre>) allow c1 = vector2d_c(1.0<metre>, 2.0<metre>) allow a2 = vector2d_a(1.0, 2.0) // should produce error allow b2 = vector2d_a(1.0, 2.0) // should produce error allow c2 = vector2d_c(1.0, 2.0)

i define class of these 3 examples (but not compile):

1)

type vector2d_b<'t, [<measure>] 'u>(x : 't<'u>, y : 't<'u>) = fellow member this.x = x fellow member this.y = y

2)

type vector2d_b<'t when 't :> 't<[<measure>]>>(x : 't<'u>, y : 't<'u>) = fellow member this.x = x fellow member this.y = y

3)

type vector2d_b<'t when 't :> 't<_>(x : 't<'u>, y : 't<'u>) = fellow member this.x = x fellow member this.y = y

writing 't equivalent writing 't<1> - <1> represents unit of measure dimensionless values, applies implicitly when no other unit of measure explicitly provided.

consequently, can't forcefulness compiler produce error message when don't explicitly provide unit of measure, since when you're implicitly providing unit of measure dimensionless values.

f# generic-programming

No comments:

Post a Comment