Skip to main content

First-Class References for .NET

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

Erik said…
I wrote something very, very similar to that last example - the one using Expression - for a simple WinForms data context binding class. Using your implementation (which I know for sure would be super-fast - your stuff is awesome like that!) would have been great.
Sandro Magi said…
Thanks, you might be interested in the static methods available under Sasa.Types. They permit extracting methods, properties, fields and constructors from simple LINQ expressions. The Ref implementation is built over this foundation.
Ernesto Alarcon said…
The stuff in Sasa framework is simply Amazing, thanks a lot Sandro!

Popular posts from this blog

async.h - asynchronous, stackless subroutines in C

The async/await idiom is becoming increasingly popular. The first widely used language to include it was C#, and it has now spread into JavaScript and Rust. Now C/C++ programmers don't have to feel left out, because async.h is a header-only library that brings async/await to C! Features: It's 100% portable C. It requires very little state (2 bytes). It's not dependent on an OS. It's a bit simpler to understand than protothreads because the async state is caller-saved rather than callee-saved. #include "async.h" struct async pt; struct timer timer; async example(struct async *pt) { async_begin(pt); while(1) { if(initiate_io()) { timer_start(&timer); await(io_completed() || timer_expired(&timer)); read_data(); } } async_end; } This library is basically a modified version of the idioms found in the Protothreads library by Adam Dunkels, so it's not truly ground bre...

Easy Automatic Differentiation in C#

I've recently been researching optimization and automatic differentiation (AD) , and decided to take a crack at distilling its essence in C#. Note that automatic differentiation (AD) is different than numerical differentiation . Math.NET already provides excellent support for numerical differentiation . C# doesn't seem to have many options for automatic differentiation, consisting mainly of an F# library with an interop layer, or paid libraries . Neither of these are suitable for learning how AD works. So here's a simple C# implementation of AD that relies on only two things: C#'s operator overloading, and arrays to represent the derivatives, which I think makes it pretty easy to understand. It's not particularly efficient, but it's simple! See the "Optimizations" section at the end if you want a very efficient specialization of this technique. What is Automatic Differentiation? Simply put, automatic differentiation is a technique for calcu...

Building a Query DSL in C#

I recently built a REST API prototype where one of the endpoints accepted a string representing a filter to apply to a set of results. For instance, for entities with named properties "Foo" and "Bar", a string like "(Foo = 'some string') or (Bar > 99)" would filter out the results where either Bar is less than or equal to 99, or Foo is not "some string". This would translate pretty straightforwardly into a SQL query, but as a masochist I was set on using Google Datastore as the backend, which unfortunately has a limited filtering API : It does not support disjunctions, ie. "OR" clauses. It does not support filtering using inequalities on more than one property. It does not support a not-equal operation. So in this post, I will describe the design which achieves the following goals: A backend-agnostic querying API supporting arbitrary clauses, conjunctions ("AND"), and disjunctions ("OR"). Implemen...