11

I just noticed that the FSharp.Core namespace includes generic array types of up to 4 dimensions, i.e. Core.[]<'T>, Core.[,]<'T>, etc. C# and VB seem to be doing just fine with System.Array - although it's not clear to me how they manage to support strongly-typed specialized array types like int[], string[].

I guess this becomes two questions then:

  • How does C# support strongly-typed specialized array types like int[] based on the non-generic System.Array?
  • Given that C# manages to do this, why does F# define generic array types?
8

F# doesn't define generic array types per se. Arrays in F# are the same array type rest of .NET uses, and despite all syntactic evidence pointing to the contrary, they're not generic.

This can sometimes backfire on you when you're using reflection in F# - even if you have an array<int> which appears to be a perfectly valid generic type, when you inspect it you get a false from IsGenericType, a true from IsArray, and you get the type argument with GetElementType rather then GetGenericArguments.

Like Telastyn noted in his answer, this is likely the legacy baggage of .NET 1.0 that we'll have to live with. But it's just one of many things that makes using reflection API in F# a real minefield.

What F# does define, are the [], [,] ... type abbreviations/aliases/operators and modules with corresponding functions, but they all ultimately refer to the same Array type beneath.

You can look at the source code on github here and here to see it for yourself.

  • But these are listed as real types, not type aliases. I'm confused because msdn.microsoft.com/en-us/library/dd233214.aspx says "The type of all F# arrays is the .NET Framework type Array", but then what is the relationship between System.Array and the FSharp.Core generic arrays? – Asik Jul 2 '14 at 21:18
  • There are no generic arrays ;) They might be real types technically, and that's why msdn doc generator picked them up, but all that they define is how the compiler should expand them, if I read into it correctly (the first file I linked), and a couple extension methods in the second file. That's all there is to it. Create one of those and call GetType on it. It'll be a specialized array type based on System.Array. – scrwtp Jul 2 '14 at 21:35
  • Or rather, the static types themselves are generic, but the runtime array type they're wrapping is not. Maybe implementing type inference is slightly easier that way, but that's a wild guess. – scrwtp Jul 2 '14 at 21:43
5

How does C# support strongly-typed specialized array types like int[] based on the non-generic System.Array?

Very likely, by generating a specialized class that inherits from System.Array since generics weren't available in 1.0. The behavior has likely been kept around for compatibility.

Given that C# manages to do this, why does F# define generic array types?

Because F# didn't have the same compability constraints that C# 2.0 had with existing code. Based on what I know, C# 2.0 would have had generic array types if it was the first release of C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.