References in C# are second-class citizens, which is to say, that you cannot pass them around as values. They can appear in function parameter position, but that's it:
public static void DoFoo(ref int value)
{
Action captureRef = () => value = 3; // ERROR: ref can't escape scope
}
This makes it easy to verify their safety statically, and makes them very efficient, but it somewhat limits their expressiveness.
First-class citizens can be passed around as values, and otherwise used in any way that you'd use any other object. The above capture would work, for instance. To make references first-class citizens means constructing an object that exposes get and set operations, and that can reference the internals of any .NET type.
IRef<T>
Sasa has had the IRef<T> type for quite some time, but its full potential as a first-class reference hasn't been realized. There was only a single implementation, and that was a simple mutable slot as found in ML. I've just pushed a series of changes that effectively expands the usefulness of IRef.
You can now create a reference to an object property, a field, and a reference into an array:
public class Ref
{
/// <summary>
/// First-class reference indexing into an array.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Array<T> : Ref, IRef<T>
...
/// <summary>
/// First-class reference into an object property.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Member<T> : Ref, IRef<T>
...
}
The static Ref
class contains a series of static, overloaded Create
methods to construct instances of the appropriate IRef type. The signatures look like this:
public class Ref
{
public static Ref<T> Create<T>(T value);
public static Ref<T> Create<T>();
public static Array<T> Create<T>(T[] array, int index);
public static Member<T> Create<T>(Func<T> get, Action<T> set);
public static IRef<T> Create<TObject, T>(TObject obj, MemberInfo member);
public static IRef<T> Create<TObject, T>(TObject obj, FieldInfo field);
public static Member<T> Create<T>(object obj, PropertyInfo property);
public static IRef<T> Create<TObject, T>(TObject obj, Expression<Func<TObject, T>> member);
}
The first two overloads were the pre-existing constructors for slots as found in MLs, like OCaml. The third overload creates a reference that indexes into the given array. The fourth overload taking two delegates creates a ref to object members, like properties and fields. For properties these delegates are precisely the get/set methods of the property, so there's no additional overhead to using them.
The overloads taking the reflection members should be obvious as creating references to those members. The last overload is simply a convenient means of specifying the member you wish to access in a type-safe manner using LINQ expressions. For instance, here's an example from the test suite:
class Foo
{
public int Bar { get; set; }
}
[Fact]
static void TestClassRef()
{
var f = new Foo { Bar = 3 };
var fref = Ref.Create(f, x => x.Bar);
Assert.Equal(f.Bar, fref.Value);
fref.Value = 99;
Assert.Equal(99, f.Bar);
Assert.Equal(f.Bar, fref.Value);
}
The reference variable fref
is constructed via a simple LINQ expression at compile-time, instead of via error-prone runtime reflection. If Foo.Bar were a field instead of a property, the above code would be unchanged. The above code is for instance members. A static member looks a little different because the reference to provide is null:
class FieldObj
{
public static int Foo = 3;
}
[Fact]
static void TestStaticFieldRef()
{
var fref = Ref.Create(default(FieldObj), x => FieldObj.Foo);
Assert.Equal(3, fref.Value);
fref.Value = 99;
Assert.Equal(99, fref.Value);
}
I use default(FieldObj) so type inference resolves the type parameters to Ref.Create, but you can use null and specify the type parameter manually if you like.
First-class references enable you to decouple programs using simple get/set semantics from the underlying representation being modified. The same algorithm exploiting IRef can transparently manipulate arrays, instance fields and properties, or static fields and properties.
These new ref types will be in the upcoming Sasa v0.12.0 release, or you can grab the source from Sourceforge and play with them now!
Comments