www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Chaining methods

reply David Medlock <amedlock nospam.org> writes:
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
next sibling parent reply zwang <nehzgnaw gmail.com> writes:
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
parent reply David Medlock <amedlock nospam.org> writes:
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
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
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
parent reply David Medlock <nospam nospam.com> writes:
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
next sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
 -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
parent David Medlock <nospam nospam.com> writes:
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
prev sibling parent Lars Ivar Igesund <larsivar igesund.net> writes:
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
prev sibling next sibling parent Ant <Ant_member pathlink.com> writes:
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
prev sibling parent Kaz. <Kaz._member pathlink.com> writes:
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