ReturnThis/@chain

dsimcha dsimcha at yahoo.com
Sat Jul 24 08:16:57 PDT 2010


== Quote from Simen kjaeraas (simen.kjaras at gmail.com)'s article
> On Sat, 24 Jul 2010 16:44:38 +0200, dsimcha <dsimcha at yahoo.com> wrote:
> > =3D=3D Quote from Tomek Sowi=C5=84ski (just at ask.me)'s article
> >> > pragma(msg, typeof(bar.val(someValue)));  // Bar, not Foo.
> >> This looks like a job for the template this parameter:
> >>      T val(this T)(int newVal) {
> >>          _val =3D3D newVal;
> >>          return cast(T) this;
> >>      }
> >> Funny enough, if you put Bar into Foo (Foo bar =3D3D new Bar; ) it  =
> >> doesn't=3D
> >>   =3D
> >> work no more.
> >> Tomek
> >
> > NO #$(&$# WAY!!!!!  I (ab)use D metaprogramming constantly and was  =
> > completely
> > unaware of template this parameters.  This is **exactly** what I neede=
> d  =
> > and I
> > can't believe it's in the language already.
> What, this is seriously already here? Why the fuck was I not informed?
> Seriously, this is one under-advertised feature.
> -- =
> Simen

...but on attempting to use it, it's rather useless in cases where you also want
to provide a getter because:

1.  You can't overload functions against templates, even in situations where this
would be completely unambiguous (see bug 2972
http://d.puremagic.com/issues/show_bug.cgi?id=2972).

2.  If you make your getter also have a template this parameter, it doesn't work
because your getter isn't returning this.


More information about the Digitalmars-d mailing list