Thread overview | |||||
---|---|---|---|---|---|
|
June 09, 2005 DMD 0.126, anonymous classes | ||||
---|---|---|---|---|
| ||||
Typical Java anonymous class patterns #1 ------------------------------------------------------- import std.stdio; Object makeObj(char[] name) { char[] objName = "My name is " ~ name; return new class Object { char[] toString() { return objName; } }; } int main() { writef ("%s\n", makeObj("Vasili").toString()); return 0; } Produces in RT: "Error: 4invalid UTF-8 sequence" and "Access Violation" - [nodebug]/debug #2 ---------------------------------------------- import std.stdio; Object makeAnotherObj(char[] name) { return new class Object { static char[] objName = "My name is"; char[] toString() { return objName ~ name; } }; } int main() { writef ("%s\n", makeAnotherObj("Vasili").toString()); return 0; } Produces in RT: "Access Violation" Andrew. |
June 09, 2005 Re: DMD 0.126, anonymous classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Fedoniouk | I understand there's a disclaimer in the documentation regarding the first example. "Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d88mok$jk2$1@digitaldaemon.com... > Typical Java anonymous class patterns > > #1 ------------------------------------------------------- import std.stdio; > > Object makeObj(char[] name) > { > char[] objName = "My name is " ~ name; > return new class Object { > char[] toString() > { > return objName; > } > }; > } > > int main() > { > writef ("%s\n", makeObj("Vasili").toString()); > return 0; > } > > Produces in RT: "Error: 4invalid UTF-8 sequence" and "Access Violation" - [nodebug]/debug > > #2 ---------------------------------------------- > import std.stdio; > > Object makeAnotherObj(char[] name) > { > return new class Object { > static char[] objName = "My name is"; > char[] toString() > { > return objName ~ name; > } > }; > } > > int main() > { > writef ("%s\n", makeAnotherObj("Vasili").toString()); > return 0; > } > > Produces in RT: "Access Violation" > > Andrew. > > > |
June 09, 2005 Re: DMD 0.126, anonymous classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | "Kris" <fu@bar.com> wrote in message news:d88n5f$k3j$1@digitaldaemon.com... >I understand there's a disclaimer in the documentation regarding the first > example. Original Java code: public Object makeObj(String name) { final String objName = "My name is " + name; return new Object() { public String toString() { return objName; } }; } Inner classes in Java can access only finals in outer function because anonymous classes store such variables in hidden fields. Second sample is from the same category - access to stack frame which is gone. Just be aware while writing converter. The problem is not in runtime. Compiler should detect such cases. Bugs like these are too hard to detect in runtime especially if you are getting "invalid UTF-8 sequence". Andrew. > > > "Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d88mok$jk2$1@digitaldaemon.com... >> Typical Java anonymous class patterns >> >> #1 ------------------------------------------------------- import std.stdio; >> >> Object makeObj(char[] name) >> { >> char[] objName = "My name is " ~ name; >> return new class Object { >> char[] toString() >> { >> return objName; >> } >> }; >> } >> >> int main() >> { >> writef ("%s\n", makeObj("Vasili").toString()); >> return 0; >> } >> >> Produces in RT: "Error: 4invalid UTF-8 sequence" and "Access Violation" - [nodebug]/debug >> >> #2 ---------------------------------------------- >> import std.stdio; >> >> Object makeAnotherObj(char[] name) >> { >> return new class Object { >> static char[] objName = "My name is"; >> char[] toString() >> { >> return objName ~ name; >> } >> }; >> } >> >> int main() >> { >> writef ("%s\n", makeAnotherObj("Vasili").toString()); >> return 0; >> } >> >> Produces in RT: "Access Violation" >> >> Andrew. >> >> >> > > |
Copyright © 1999-2021 by the D Language Foundation