Monday, May 09, 2005

Arrays, sets, brackets and curly braces

I'm afraid there're some big changes on the way, in order to accommodate both array and set literal constructors in Freya. We gave priority to the implementation of arrays, in order to match C#'s functionality as soon as possible. You can write "literal arrays" in Freya using a bracket-delimited list:
[1, 2, i]
That would be the equivalent of C#'s:
new int[] { 1, 2, i }
Freya's notation is shorter, but we have to check yet how it behaves together with method overloading. C#'s on-the-fly arrays has a clear hint about the array type. I'm already inferring the array type from the item types. This is not a problem with assignments. As in C#'s, Freya arrays are covariant. When a covariant array assignment is detected, the compiler checks whether the right side is a literal array constructor, as above, in order to perform the conversion at compile time.
Why do I mention method overloading? Well, overload resolution has a very detailed specification for C#, and most of it has been borrowed by Freya. I still have to prove that the potential ambiguity introduced by these array expressions doesn't break the algorithm. I hope it won't, since all examples tried by hand have succeeded.
And, what about sets? One of the goals for Freya is preserving the Set data type from Pascal, with some minor changes, such as introducing an imaginary Set[X] generic class, and a big change: Freya sets won't be limited to integer subranges and enumerative types.
One of our problems is how we will write literal set constructors. Pascal uses brackets as delimiters, and Freya could do the same. That would bring more ambiguity to bracket delimited expressions, and I'm not happy with the preliminary results. That's the reason why I'm thinking about reintroducing curly braces as set delimiters (the role they already play in mathematical notation!). This would imply changes in multiline comments. Delphi borrowed single line comments from C++, so I don't see any reason why I shouldn't use C# delimiters for multiline comments in Freya:
/* A Freya multiline comment */
if i in {1,2,3} then
if j in [1, 2, 3] then
// ...
Note that we already allow the in operator along with arrays. The second condition is translated this way:
if [1,2,3].IndexOf(j) <> -1 then
// ...
Of course, since this example features a constant array, the actual translation would be:
if j = 1 or j = 2 or j = 3 then
// ...
Note that, thanks to our new and sounder precedence rules, we didn't need any parenthesis in the Boolean expression.

Labels: , ,

0 Comments:

Post a Comment

<< Home