jeudi 23 octobre 2014

f# function call with parameters and cast to units of measure


Vote count:

0




I like to write some math function in F' and use them from C#. As F# uses units of measures it is really greate to have the compile time check. In C# I already have a Length (indexer that converts to m, mm, km etc) class so I want to us that as a parameter from C' using the F# function and convert it there to float or more specific float. As I have two parameters I struggel calling the functions. I guess it has something to do with currying.



module Static
open Xunit
open Units.Contract // here is my Length class defined in C#

[<Measure>] type m
[<Measure>] type slabThickness = m
[<Measure>] type kN
[<Measure>] type freshConcreteDensity = kN/m^3
[<Measure>] type freshConcreteLoad = kN/m^2


let FreshConcreteLoad(slabThickness:float<m>, freshConcreteDensity:float<kN/m^3>) = slabThickness * freshConcreteDensity // 1. works

let FreshConcreteLoadL(slabThickness:Length, freshConcreteDensity:Length) = slabThickness.[Length.Units.m] * freshConcreteDensity.[Length.Units.m] //2. works

let FreshConcreteLoadLUseMeasure(slabThickness:Length, freshConcreteDensity:Length) = FreshConcreteLoad (slabThickness.[Length.Units.m]:float<m> freshConcreteDensity.[Length.Units.m]:float<kN/m^3>) //3. here I struggel

// Related to 1. and works
[<Fact>]
let FreshConcreteLoad_Test() =
let thickness = 0.2<slabThickness>
let density = 25.0<freshConcreteDensity>
let load:float<freshConcreteLoad> = FreshConcreteLoad(thickness,density)
Assert.Equal(load, 5.0<kN/m^2>)

// Related to 2. and works
[<Fact>]
let FreshConcreteLoadL_Test() =
let thickness = new Length(0.2)
let density = new Length(25.0) // dont care that this is now also a length, just for testing. in real live here would be a class for kN
let load:float = FreshConcreteLoadL(thickness,density)
Assert.Equal(load, 5.0)

// Related to 3. and I struggle with the function call
[<Fact>]
let FreshConcreteLoadLUseMeasure_Test() =
let thickness = new Length(0.2)
let density = new Length(25.0) // dont care that this is now also a length, just for testing. in real live here would be a class for kN
let load:float = FreshConcreteLoadLUseMeasure(thickness,density)
Assert.Equal(load, 5.0)


asked 1 min ago

KCT

1






f# function call with parameters and cast to units of measure

Aucun commentaire:

Enregistrer un commentaire