Auto syntax revisited

James James_member at pathlink.com
Tue Feb 21 00:05:01 PST 2006


Re auto for implicit typing. 
I dont know if C# 3.0 seems to be going in the same direction - but with a
different keyword.
>From Language Spec at http://msdn.microsoft.com/vcsharp/future/default.aspx 

Implicitly typed local variables 

In an implicitly typed local variable declaration, the type of the local
variable being declared is inferred from the expression used to initialize the
variable. When a local variable declaration specifies var as the type and no
type named var is in scope, the declaration is an implicitly typed local
variable declaration. For example:

var i = 5; 
var s = "Hello"; 
var d = 1.0; 
var numbers = new int[] {1, 2, 3}; 
var orders = new Dictionary<int,Order>();

The implicitly typed local variable declarations above are precisely equivalent
to the following explicitly typed declarations:

int i = 5; 
string s = "Hello"; 
double d = 1.0; 
int[] numbers = new int[] {1, 2, 3}; 
Dictionary<int,Order> orders = new Dictionary<int,Order>();

A local variable declarator in an implicitly typed local variable declaration is
subject to the following restrictions:

The declarator must include an initializer. 
The initializer must be an expression. The initializer cannot be an object or
collection initializer (§26.4) by itself, but it can be a new expression that
includes an object or collection initializer. 
The compile-time type of the initializer expression cannot be the null type. 
If the local variable declaration includes multiple declarators, the
initializers must all have the same compile-time type. 
The following are examples of incorrect implicitly typed local variable
declarations:

var x;     // Error, no initializer to infer type from 
var y = {1, 2, 3}; // Error, collection initializer not permitted 
var z = null;   // Error, null type not permitted

~~~~~~~~~~~
Sometimes i think it would be useful to take mono's c# front end and put it on
gcc and make it a compiled language. With the additional of auto for stack based
deterministic finalisation for objects.  







More information about the Digitalmars-d mailing list