digitalmars.D - Chaining methods
- David Medlock (14/14) Apr 11 2005 Not sure if this has been brought up, but why does D not allow chaining
- zwang (3/22) Apr 11 2005 Try this instead:
- David Medlock (6/31) Apr 11 2005 Thanks for the tip, but the question I am asking is why doesn't the D
- Lars Ivar Igesund (7/44) Apr 11 2005 No, it's not. If you ignore the fact that 'new' is more like a function
- David Medlock (20/71) Apr 11 2005 It is not in _this_ implementation, but I can tell you in most compilers...
- Andrew Fedoniouk (25/99) Apr 11 2005 What compiler do you use?
- David Medlock (2/127) Apr 11 2005 I used Digital Mars C++ ;)
- Lars Ivar Igesund (10/93) Apr 13 2005 Sorry for sortof misunderstanding your original point. I agree that your...
- Ant (5/19) Apr 11 2005 do:
- Kaz. (17/31) Apr 11 2005 In D,
Not sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -David
Apr 11 2005
David Medlock wrote:Not sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 11 2005
zwang wrote:David Medlock wrote:Thanks for the tip, but the question I am asking is why doesn't the D compiler allow the syntax I posted? From an AST perspective, the type of 'new A()' is the same as a variable of type A. More a question for Walter I guess. -DavidNot sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 11 2005
David Medlock wrote:zwang wrote:No, it's not. If you ignore the fact that 'new' is more like a function (overloadable in a class), then; A is a class, and A.Size() means that Size is a static method in A. (new A) is an instance of type A, and (new A).Size() is a method probably needing the context in which the object was instantiated. Lars Ivar IgesundDavid Medlock wrote:Thanks for the tip, but the question I am asking is why doesn't the D compiler allow the syntax I posted? From an AST perspective, the type of 'new A()' is the same as a variable of type A. More a question for Walter I guess. -DavidNot sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 11 2005
Lars Ivar Igesund wrote:David Medlock wrote:It is not in _this_ implementation, but I can tell you in most compilers the *type* of the new expression is a variable of type A, which should be able to dereference. If you disagree, tell me where the result of a new expression is not valid and a class reference is. -David Method chaining is actually a C++ idiom. Here's a C++ version, which compiles fine: class A { public: int SZ ; A() : SZ(0) { } int Size() { return SZ; } }; int main( int argc, char** arg ) { int var = new A()->Size(); }zwang wrote:No, it's not. If you ignore the fact that 'new' is more like a function (overloadable in a class), then; A is a class, and A.Size() means that Size is a static method in A. (new A) is an instance of type A, and (new A).Size() is a method probably needing the context in which the object was instantiated. Lars Ivar IgesundDavid Medlock wrote:Thanks for the tip, but the question I am asking is why doesn't the D compiler allow the syntax I posted? From an AST perspective, the type of 'new A()' is the same as a variable of type A. More a question for Walter I guess. -DavidNot sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 11 2005
-David Method chaining is actually a C++ idiom. Here's a C++ version, which compiles fine:What compiler do you use? VC++ from 6.0 up to VC++ 2005/Widbey produce following: Compiling... chain.cpp c:\tests\chain\chain.cpp(17) : error C2440: 'initializing' : cannot convert from 'A *' to 'int' There is no context in which this conversion is possible c:\tests\chain\chain.cpp(17) : error C2143: syntax error : missing ';' before '->' while compiling class A { public: int SZ ; A() : SZ(0) { } int Size() { return SZ; } }; int main( int argc, char** arg ) { int var = new A()->Size(); return var; } "David Medlock" <nospam nospam.com> wrote in message news:d3es51$jkb$1 digitaldaemon.com...Lars Ivar Igesund wrote:David Medlock wrote:It is not in _this_ implementation, but I can tell you in most compilers the *type* of the new expression is a variable of type A, which should be able to dereference. If you disagree, tell me where the result of a new expression is not valid and a class reference is. -David Method chaining is actually a C++ idiom. Here's a C++ version, which compiles fine: class A { public: int SZ ; A() : SZ(0) { } int Size() { return SZ; } }; int main( int argc, char** arg ) { int var = new A()->Size(); }zwang wrote:No, it's not. If you ignore the fact that 'new' is more like a function (overloadable in a class), then; A is a class, and A.Size() means that Size is a static method in A. (new A) is an instance of type A, and (new A).Size() is a method probably needing the context in which the object was instantiated. Lars Ivar IgesundDavid Medlock wrote:Thanks for the tip, but the question I am asking is why doesn't the D compiler allow the syntax I posted? From an AST perspective, the type of 'new A()' is the same as a variable of type A. More a question for Walter I guess. -DavidNot sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 11 2005
Andrew Fedoniouk wrote:I used Digital Mars C++ ;)-David Method chaining is actually a C++ idiom. Here's a C++ version, which compiles fine:What compiler do you use? VC++ from 6.0 up to VC++ 2005/Widbey produce following: Compiling... chain.cpp c:\tests\chain\chain.cpp(17) : error C2440: 'initializing' : cannot convert from 'A *' to 'int' There is no context in which this conversion is possible c:\tests\chain\chain.cpp(17) : error C2143: syntax error : missing ';' before '->' while compiling class A { public: int SZ ; A() : SZ(0) { } int Size() { return SZ; } }; int main( int argc, char** arg ) { int var = new A()->Size(); return var; } "David Medlock" <nospam nospam.com> wrote in message news:d3es51$jkb$1 digitaldaemon.com...Lars Ivar Igesund wrote:David Medlock wrote:It is not in _this_ implementation, but I can tell you in most compilers the *type* of the new expression is a variable of type A, which should be able to dereference. If you disagree, tell me where the result of a new expression is not valid and a class reference is. -David Method chaining is actually a C++ idiom. Here's a C++ version, which compiles fine: class A { public: int SZ ; A() : SZ(0) { } int Size() { return SZ; } }; int main( int argc, char** arg ) { int var = new A()->Size(); }zwang wrote:No, it's not. If you ignore the fact that 'new' is more like a function (overloadable in a class), then; A is a class, and A.Size() means that Size is a static method in A. (new A) is an instance of type A, and (new A).Size() is a method probably needing the context in which the object was instantiated. Lars Ivar IgesundDavid Medlock wrote:Thanks for the tip, but the question I am asking is why doesn't the D compiler allow the syntax I posted? From an AST perspective, the type of 'new A()' is the same as a variable of type A. More a question for Walter I guess. -DavidNot sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 11 2005
David Medlock wrote:Lars Ivar Igesund wrote:Sorry for sortof misunderstanding your original point. I agree that your interpretation could have worked (logically), but I think that D's position on ease of implementation might hinder it, as there probably is several possible interpretations (as I tried to show in my last post) that might conflict. Also, whether it works or not in C++ is rather unimportant IMO. I hadn't seen it before, myself (not the most experienced C++-programmer out there, but anyway). Lars Ivar IgesundDavid Medlock wrote:It is not in _this_ implementation, but I can tell you in most compilers the *type* of the new expression is a variable of type A, which should be able to dereference. If you disagree, tell me where the result of a new expression is not valid and a class reference is. -David Method chaining is actually a C++ idiom. Here's a C++ version, which compiles fine: class A { public: int SZ ; A() : SZ(0) { } int Size() { return SZ; } }; int main( int argc, char** arg ) { int var = new A()->Size(); }zwang wrote:No, it's not. If you ignore the fact that 'new' is more like a function (overloadable in a class), then; A is a class, and A.Size() means that Size is a static method in A. (new A) is an instance of type A, and (new A).Size() is a method probably needing the context in which the object was instantiated. Lars Ivar IgesundDavid Medlock wrote:Thanks for the tip, but the question I am asking is why doesn't the D compiler allow the syntax I posted? From an AST perspective, the type of 'new A()' is the same as a variable of type A. More a question for Walter I guess. -DavidNot sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -DavidTry this instead: int var = (new A()).Size();
Apr 13 2005
In article <d3e5mt$2ooo$1 digitaldaemon.com>, David Medlock says...Not sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -Daviddo: int var = (new A()).Size(); now ask the next question. Ant
Apr 11 2005
In D, new foo.bar is parsed as: new (foo.bar) not (new foo).bar. Maybe this behaviour is for instanciating nested classes: class A { class B {} } void main() { A a = new A; A.B b1 = new A.B; // A.B b2 = new a.B; // } In article <d3e5mt$2ooo$1 digitaldaemon.com>, David Medlock says...Not sure if this has been brought up, but why does D not allow chaining method calls from a constructor? class A { int SZ = 0; this() { SZ = 0; } int Size() { return SZ; } } void main( char[][] arg ) { int var = new A().Size(); } chain.d(10): semicolon expected, not '.' -David-- Kazuhiro Inaba
Apr 11 2005