digitalmars.D.learn - method chaining
- spir (26/26) Nov 08 2010 Hello,
- Steven Schveighoffer (7/27) Nov 08 2010 To the compiler, this means:
- Tomek =?UTF-8?B?U293acWEc2tp?= (4/30) Nov 08 2010 C c = (new C(1)).set(3);
Hello, I don't understand why the compiler refuses the code below, with the error __trials__.d(33): found '.' when expecting ';' following statement (Note that method set returns this.) class C { int i,j; this (int i) { this.i =3D i; } C set (int j) { this.j =3D j; return this; } override string toString () { return format("C(%s,%s)", i,j); } } void main () { c =3D new C(1).set(3); /////// writeln(c); } Well, the example is somewhat artificial, but this idiom is highly useful. Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 08 2010
On Mon, 08 Nov 2010 14:39:43 -0500, spir <denis.spir gmail.com> wrote:Hello, I don't understand why the compiler refuses the code below, with the error __trials__.d(33): found '.' when expecting ';' following statement (Note that method set returns this.) class C { int i,j; this (int i) { this.i = i; } C set (int j) { this.j = j; return this; } override string toString () { return format("C(%s,%s)", i,j); } } void main () { c = new C(1).set(3); ///////To the compiler, this means: c = new (C(1).set(3)); What you want is: c = (new C(1)).set(3); Java implies this, but D does not. -Steve
Nov 08 2010
spir napisaĆ:Hello, I don't understand why the compiler refuses the code below, with the error __trials__.d(33): found '.' when expecting ';' following statement (Note that method set returns this.) class C { int i,j; this (int i) { this.i = i; } C set (int j) { this.j = j; return this; } override string toString () { return format("C(%s,%s)", i,j); } } void main () { c = new C(1).set(3); /////// writeln(c); } Well, the example is somewhat artificial, but this idiom is highly useful.C c = (new C(1)).set(3); -- Tomek
Nov 08 2010