programming, .NET, linux and others

Out reference parameters in F#

by Gregor Uhlenheuer on December 3, 2012

Lately I wanted to implement a class that derives from DynamicObject (in the System.Dynamic namespace) and therefore I had to override the methods TrySetMember and TryGetMember. Especially the latter one forced me to look up on reference parameters in F#.

The method signature of TryGetMember looks like the following (in C# syntax):

public virtual bool TryGetMember(
    GetMemberBinder binder,
    out object result

In F# you have to declare the result parameter as a reference type with byref:

open System.Runtime.InteropServices

override x.TryGetMember (binder : GetMemberBinder, [<Out>] result : byref<obj>) =
    raise <| NotImplementedException()

I am not quite sure if the [<Out>] parameter attribute is really necessary since my project compiled just fine without it as well.

This post is tagged with f#, .net and programming