using this instead of typeof(this)
Maxim Fomin
maxim at maxim-fomin.ru
Tue Feb 26 09:28:12 PST 2013
On Tuesday, 26 February 2013 at 16:28:59 UTC, Ivan Kazmenko wrote:
> Hi!
>
> I have recently experimented with ways to express the exact
> instantiated type of a generic struct, and found that I can in
> some cases use "this" as that type.
>
1) D allows omitting parameter names in function definitions
which are replaced by implicitly created _param_XXX.
2) This as a type in parameter list replaces type of this.
What you observe is consequence of combination of this rules. The
most unintuitive is that applying attribute to postblit
declaration makes it a regular constructor which takes argument
of that struct type.
>
> And so I wonder:
>
> (1) Should I refrain from using this as a type, is it a bug?
Better to avoid such usage.
> (2) A matter of style: what is the idiomatic way to take the
> exact type of a templated struct? For example, which method
> signature to return a typeof(this) value is "better" in which
> way if all have the same effect:
> -----
> struct S (A, B, C)
> {
> ...
> auto method () {...}
> S method () {...}
> S !(theA, theB, theC) method () {...}
> typeof (this) method () {...}
> }
> -----
> Note that S, theA, theB and theC can get lengthy.
You can use template this parameter in templates and avoid in
non-templates.
> (3) Is the usage of unnamed parameters and _param_### a
> language feature or an implementation-specific detail I should
> not ever use?
>
> -----
> Ivan Kazmenko.
It is implementation-specific detail.
More information about the Digitalmars-d-learn
mailing list