"I am receiving objects of type..."

This sounds dubious to me. It would be useful to print out

1
x.GetType().Name

and

1
x.[0].GetType().Name

to see what you really have. And that should drive the 'casting' via the ":?>" operator. Whatever x.GetType().Name reports, cast x to that via :?>. And then if it's an array, whatever it's zeroeth element's type is, Array.map a :?> cast to that type for all the elements.

By on 10/4/2010 6:26 PM ()

Maybe they are COM multidimensional variant SAFEARRAYs and not .net objects?

By on 10/5/2010 9:33 AM ()

My apologies for not being clear. I am attempting to write a function that takes arrays of various types and then returns a 2-tuple of that type (ie 'a array array -> ('a * 'a) array). I thought the way to test for an array type was to do something like the following:

1
2
3
4
5
6
let fn (a:'a array) : ('a * 'a) ->
   match a with
   | :? array<float> as a ->
      ....blah blah blah...
   | :? array<DateTime> as a ->
      ....blah blah blah...

However, this is refusing to compile. brianmcn, you bring up a good point that I could use GetType to find the type of the first element and match off of that. I was just expecting there to be a more explicit way to type test arrays.

By on 10/11/2010 4:04 PM ()

The function you just described doesn't need runtime type tests, e.g. see the code below.

I still have no idea what you are trying to do.

1
2
3
4
5
6
/ generics
let f (arr: 'a[]) : ('a * 'a) =
    arr.[0], arr.[1]
 
printfn "%A" (f [| 1; 2 |])
printfn "%A" (f [| 1.1; 2.2 |])
By on 10/11/2010 4:23 PM ()

Thanks for your reply, brianmcn. I am trying do something different for each type of array, hence the match statement. Specifically, I'm trying to find the minimum and maximum order of magnitude of each array.

So for floats, singles, etc, I am doing

1
2
3
let l = Array.map log10 arr

(min l, max l)

While for DateTime arrays, I am doing something like:

1
2
3
4
let m = Array.max arr
let n = Array.min arr
let d = m - n
if d.Days > 365 then ...

So I need to match on the type of array to decide what I am doing. Hope this makes sense.

By on 10/11/2010 4:44 PM ()

What do you originally have? An "obj[]"? Or an "obj" that is actually an "int[]" or a "DateTime[]" or whatever? (Or something else, though I don't know what else it might be?)

By on 10/11/2010 5:35 PM ()

The original object was decoded from the network socket as an obj. Please pardon my lack of experience with generalization in F#, but it sounds like 'a array is not actually a type that a variable can have, but rather more like a template in C++, so it is not possible to pass a generalized array variable to a function. Is that correct?

So even though the function will satisfy the constraint that it will return tuples of the same basic type as the array that was passed in, the compiler isn't happy because one cannot downcast a generalized type?

Sorry for such a newbie question. Thanks again for the responses.

By on 10/12/2010 8:09 AM ()

Yes, F#/.NET generics are somewhat like C++ templates; all actual objects have some concrete type, rather than a generalized one.

Does this code help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    open System
 
    let gInt (arr: int[]) =
        Seq.max arr, Seq.min arr
 
    let gDateTime (arr: DateTime[]) =
        arr.[0].AddDays(1.0), arr.[0].AddDays(2.0)
 
    let g (o:obj) =
        match o with
        | :? (int[]) as ia -> printfn "%A" (gInt ia)
        | :? (DateTime[]) as dta -> printfn "%A" (gDateTime dta)
        | _ -> printfn "unknown"
 
    let networkData1 = box [| 1;2;3 |]
    let networkData2 = box [| DateTime.Now |]
 
    g networkData1 
    g networkData2
By on 10/12/2010 9:12 AM ()

Thanks, brianmcn. This does help a lot. I guess what I am realizing is that using 'a[] as an intermediate type is actually confusing things as you can't bind a variable of that type. So writing:

1
2
3
4
5
let g (o:obj) : (arr: 'a[]) =
match o with
| :? (int[]) as ia -> ia
| :? (DateTime[]) as dta -> dta
| _ -> [| |]

isn't possible. Thanks again for the help and the sample code.

By on 10/12/2010 9:25 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