digitalmars.D - Chaining methods
- David Medlock <amedlock nospam.org> Apr 11 2005
- zwang <nehzgnaw gmail.com> Apr 11 2005
- David Medlock <amedlock nospam.org> Apr 11 2005
- Lars Ivar Igesund <larsivar igesund.net> Apr 11 2005
- David Medlock <nospam nospam.com> Apr 11 2005
- "Andrew Fedoniouk" <news terrainformatica.com> Apr 11 2005
- David Medlock <nospam nospam.com> Apr 11 2005
- Lars Ivar Igesund <larsivar igesund.net> Apr 13 2005
- Ant <Ant_member pathlink.com> Apr 11 2005
- Kaz. <Kaz._member pathlink.com> Apr 11 2005
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 '.' -David
Try this instead: int var = (new A()).Size();
Apr 11 2005
zwang wrote: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 '.' -David
Try this instead: int var = (new A()).Size();
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. -David
Apr 11 2005
David Medlock wrote:zwang wrote: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 '.' -David
Try this instead: int var = (new A()).Size();
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. -David
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 Igesund
Apr 11 2005
Lars Ivar Igesund wrote:David Medlock wrote:zwang wrote: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 '.' -David
Try this instead: int var = (new A()).Size();
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. -David
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 Igesund
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(); }
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:zwang wrote: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 '.' -David
Try this instead: int var = (new A()).Size();
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. -David
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 Igesund
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(); }
Apr 11 2005
Andrew Fedoniouk wrote:-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:zwang wrote: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 '.' -David
Try this instead: int var = (new A()).Size();
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. -David
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 Igesund
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(); }
Apr 11 2005
David Medlock wrote:Lars Ivar Igesund wrote:David Medlock wrote:zwang wrote: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 '.' -David
Try this instead: int var = (new A()).Size();
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. -David
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 Igesund
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(); }
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 Igesund
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 '.' -David
do: 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









David Medlock <nospam nospam.com> 