This isn't allowed because of F# strong typing, you can't make a function that accepts an int array or float array. However you can create a function that accepts a union type that can be either ints or floats:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
open System.Runtime.InteropServices

[<DllImport(@"netcdf.dll",EntryPoint="nc_get_var_int")>]
extern int nc_get_var_int_(int ncid,int varid,int[] ip)

[<DllImport(@"netcdf.dll",EntryPoint="nc_get_var_float")>]
extern int nc_get_var_float_(int ncid,int varid,float[] fp)

[<DllImport(@"netcdf.dll",EntryPoint="nc_get_var_double")>]
extern int nc_get_var_double_(int ncid,int varid,double[] dp)

type Values =
 | Ints of int[]
 | Floats of float[]
 | Doubles of double[]

let fetchData ncid varid xtype =
    match xtype with
    | Ints x -> nc_get_var_int_(ncid, varid, x)
    | Floats x -> nc_get_var_float_(ncid, varid, x)
    | Doubles x -> nc_get_var_double_(ncid, varid, x)

You would then use this function as follows:

1
let _ =  fetchData 1 1 (Ints [| 1; 1; 1; |])

Cheers,
Rob

By on 8/13/2008 9:01 AM ()

Create type Values is good... but in pratice how get array parts

if I create a type Values

let u = (Ints [| 1; 1; 1; |])
or
let u = (Ints (Array.create 3 1))

now is not possible write:

let v = u.[0]

because u is not a "normal" array, i.e. int array

I guess create a getValues

let getValues thisValues =
match thisValues with
| Ints x -> x
| Floats x -> x
| Doubles x -> x

but is not allowed because of F# strong typing

Evidently, for me F# is not easy ;-)

Do you have hint for get x from value (Ints x) ?

By on 8/14/2008 4:51 AM ()

I see you're point, but the answer is simply not construct the Ints part until your ready to call your funtion i.e.:

1
2
3
let u = Array.create 3 1
let v = u.[0]
do fetchData 1 1 (Ints u)
By on 8/14/2008 10:11 PM ()

Ok,

I created a variable
let v = (Ints u) or = (Floats u) or (Doubles u)
where u is an array int[] or float32[] or double[]

and use this variable with call
fectchData 1 1 v

and my fetched values are included in array v... a variable of type Ints our Floats or Doubles

bad design ...

By on 8/15/2008 5:00 AM ()

Right, it's impossible for me to offer design advice with out a complete over view of what you trying do achive, without that I can only answer questions specific problems. Indeed a union type may not be the best option in this case, but its hard to say that definitively without understanding why there are three different versions of the methods your trying to call accepting different data types and why you would want to use them and probably most importantly what your trying to do with the result.

Bottom line is F# it a strongly typed language so an int cannot be treaded as a double, you have do some explicit conversion. How about something like:

1
2
3
4
5
6
7
8
9
10
type Values =
 | Ints of int[]
 | Doubles of double[]

let fetchData ncid varid xtype =
    match xtype with
    | Ints x -> let res = nc_get_var_int_(ncid, varid, x)
        Array.map double x 
    | Doubles x -> nc_get_var_double_(ncid, varid, x)
        x

This way you send it an array of any type but always can your result back as an array of doubles and since we can encode an int as double with out much loss of accuracy you should be okay to tread your result in the same way.

Cheers,
Rob

By on 8/15/2008 5:50 AM ()

Thank you.

Now I understand better F #.

I progress in my project.

Obsevation:

type "float" is synonym of "double".

Now I specify float32

By on 8/13/2008 7:18 PM ()

oh and if you just want to alias a function you can say:

1
let getVarInt = nc_get_var_int_
By on 8/13/2008 9:03 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper