Class-related queries [was: Re: 'Undefined reference' linking errors]

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Fri Apr 9 14:33:17 PDT 2010


Joseph Wakeling wrote:
>> Also try:
>>
>> foreach(uint i;0..10000) {
>>    scope Foo f = new Foo(i);
>>    // Do something with f ...
>> }
>>
>> That sometimes doesn't require allocations.
> 
> To be sure I understand -- is there anything wrong with writing
> 
>      scope auto f = new Foo(i)
> 
> ... or, in general, using auto to infer the class being initiated?  I found myself
> disliking the double writing of Foo, although I guess there could be a positive
> side to it in ensuring that the class really is what it's meant to be.

You don't have to write 'auto' to get type inference.  You just have to 
have something that signifies to the compiler that a variable is 
declared.  All of these are valid:

    auto x = 123;         // x is int
    const y = 1.23;       // y is const(double)
    static z = "hello";   // z is string
    scope f = new Foo(i); // f is Foo

It is a common belief that 'auto' means automatic type inference.  It 
doesn't.  'auto' is a storage class, the same as in C:

http://publib.boulder.ibm.com/infocenter/macxhelp/topic/com.ibm.vacpp6m.doc/language/ref/clrc03autdef.htm

-Lars


More information about the Digitalmars-d-learn mailing list