[Journal - Nested Array Syntax]

Nested Array Syntax

Saturday, February 12, 2005

I gotta say that I find the array declaration syntax in the two .NET languages not very intuitive as far as nested arrays are concerned. The outermost array's rank specifier is declared left-most:

string[,,][,][] a;

I always (wrongly) read this right-to-left, because the ultimate (innermost) element type stands at the begin ("string", in this case), so the rank bracketry should go outward left-to-right. However, I see that using an expression of a jagged array is easier if the outermost indices are written first (especially if you only talk to the outer elements), and so consistency wins out (however, having prefix notation for element access would lead us to a different conclusion).

So that's just me. What's more intersting is that System.Reflection and the languages don't agree:

// <addin:build?C#,1>
// <macro:Gregor.M.Foo>
public static void Foo(){

    WebEditApp.Trace(Type.GetType("System.String[,,][,][]").ToString());
    // System.String[,,][,][]

    WebEditApp.Trace(typeof(string[,,][,][]).ToString());
    // System.String[][,][,,]

    CCodeOptions options = new CCodeOptions();
    options.UseFullNames = true;
    WebEditApp.Trace(Reflect.GetTypeName(typeof(string[,,][,][]),
                     new CCSharpSyntaxProvider(),
                     options));
    // System.String[,,][,][]
}

The Gregor.Core.Reflect.GetTypeName function gets a type name string that resembles the language syntax, so there's a solution.