1

I want to be able generate random numbers of a specific range that are measures where <'T> could be [<Measure>] type X or [<Measure>] type Y

let GenerateLine<'T> (rnd:System.Random, min:int<_>, max:int<_>) : (int<_> * int<_>) =
  let v1:int< 'T > = rnd.Next(int(min),int(max)+1)
  let v2:int< 'T > = rnd.Next(int(min),int(max)+1)
  (v1,v2)

I've also tried the same above with the inline keyword after let

and

let GenerateLine (rnd:System.Random, min max) =
  let v1 = rnd.Next(min,max+1) * LanguagePrimitives.GenericOne
  let v2 = rnd.Next(min,max+1) * LanguagePrimitives.GenericOne
  (v1,v2)

also

let GenerateLine< ^T> (rnd:System.Random, min:< ^T >, max:< ^T > ) =

but I seem to be having no figuring this out or following the logic behind things like How to write a function for generic numbers?

How do you write a method/function that is generic across measures?

Community
  • 1
  • 1
Maslow
  • 18,464
  • 20
  • 106
  • 193

1 Answers1

1

Here's what I meant in my comment:

let GenerateLine (rnd:System.Random, min:int<'t>, max:int<'t>) : (int<'t> * int<'t>) =
  let v1 = rnd.Next(int(min),int(max)+1) |> LanguagePrimitives.Int32WithMeasure
  let v2 = rnd.Next(int(min),int(max)+1) |> LanguagePrimitives.Int32WithMeasure
  (v1,v2)

I've moved some of your annotations around, but you could mostly stick to what you had. The one critical thing is that if you want to give your function an explicit generic parameter, you need to mark it with the Measure attribute:

let GenerateLine<[<Measure>]'t> ...

or else the compiler expects it to be a regular generic type parameter. But I generally prefer to let type inference handle the fact that the function is generic anyway.

kvb
  • 54,864
  • 2
  • 91
  • 133
  • +1 with this I was able to remove all type annotations and types from the function definition besides the `System.Random` guy. – Maslow Jul 03 '14 at 23:13