How can I implement the same behavior as Dictionary.TryGetValue

Posted by pblasucci on Stack Overflow See other posts from Stack Overflow or by pblasucci
Published on 2010-04-27T22:03:01Z Indexed on 2010/04/27 22:13 UTC
Read the original article Hit count: 251

Filed under:
|
|

So, given then following code

type MyClass () =
  let items = Dictionary<string,int>()
  do 
    items.Add ("one",1)
    items.Add ("two",2)
    items.Add ("three",3)
  member this.TryGetValue (key,value) =
    items.TrygetValue (key,value)
let c = MyClass () 

let d = Dictionary<string,int> ()
d.Add ("one",1)
d.Add ("two",2)
d.Add ("three",3)

And the following test code

let r1,v1 = d.TryGetValue "one"
let r2,v2 = c.TryGetValue "one"

The r1,v1 line works fine. The r2,v2 line bombs; complaining c.TryGetValue must be given a tuple. Interestingly, in each line the signature of TryGetValue is different. How can I get my custom implementation to exhibit the same behavior as the BCL version? Or, asked another way, since F# has (implicitly) the concept of tuple parameters, curried parameters, and BCL parameters, and I know how to distinguish between curried and tuple-style, how can I force the third style (a la BCL methods)?

Let me know if this is unclear.

© Stack Overflow or respective owner

Related posts about F#

Related posts about .NET