开发者

F# unique overload method

开发者 https://www.devze.com 2023-01-24 03:40 出处:网络
How am I able to call send(a:\'T []) in such a type? type Test() = member o.send(value:\'T) = 4 member o.send(values:\'T []) = 5

How am I able to call send(a:'T []) in such a type?

type Test() =
  member o.send(value:'T) = 4
  member o.send(values:'T []) = 5

let test = Test()
let b = test.send [|4|]

When I do that, I obtain

A unique overload for method 'Send' could not be determined based on type information 
prior to this program point. The available overloads are shown below...

The point is the MPI.NET 开发者_如何学Gohas got exactly this method called Send and I am not able to send an array into it.

Thank you,

Oldrich


type Test() =
  member o.send(a:'T) = 4
  member o.send(a:'T []) = 5

let test = Test()
let b = test.send<int>([|4|] : _[]) // 5


Well, F# can't determine if your T is an int, or an array of ints, so the only way I can see to work round your problem is like this

let b = test.send<int> [|4|]

or this

let b = test.send<int array> [|4|]


You can do this by using pattern matching on types:

type MyTest() =
    member this.Send (a : obj)  = 
        match a with
        | :? System.Collections.IEnumerable -> 5
        | _ -> 4

This will match on any Seq type in F# (array, list, seq). Once you have the right match is is usually easier to implement private functions for the specific type.

0

精彩评论

暂无评论...
验证码 换一张
取 消