auto
Part of KeywordsCategory
Description
Shows how auto objects differ from non-auto objects. Note how the auto objects are destroyed as soon as they go out of scope. ScopeExample shows the same result by using the new usage of the scope keyword. In the future, the auto keyword is expected to be limited to automatic type deduction.
Example
import std.stdio; class A { private char[] name; char[] toString () { return name; } this (char[] s) { name = s; writefln("%s created", name); } ~this () { writefln("%s destroyed", name); } } void test2 () { auto A a2 = new A("test2"); } void test3 () { A a3 = new A("test3"); } void test4 () { auto A a4 = new A("test4"); } void main () { A a = new A("main test"); writefln("\tbegin main body"); test2(); test3(); test4(); writefln("\tend main body (garbage collector will run next...)"); }
Sample Batch File
@echo off set pgm=AutoExample dmd %pgm%.d %pgm%.exe pause erase %pgm%.obj erase %pgm%.map
Output
main test created
begin main body
test2 created
test2 destroyed
test3 created
test4 created
test4 destroyed
end main body (garbage collector will run next...)
test3 destroyed
main test destroyed
Compatibility
- Tested with DMD 0.174 on Windows 2000.
- This usage of the auto keyword probably won't work in future versions of DMD (after DMD 0.174).
