Explicit this for methods

Paul Backus snarwin at gmail.com
Sat Jul 27 00:08:19 UTC 2024


On Tuesday, 23 July 2024 at 11:07:54 UTC, vit wrote:
> Explicit `this` allow manipulating of `this` type without 
> interacting with function attributes and without need to create 
> template function or string mixin-s.
>
> Syntax:
> - explicit `this` must be first parameter of non-static method
> - name is not identifier but keyword `this`.

Agree with all of this.

> - explicit `this` doesn't support all parameter attributes like 
> `ref`, `out`, `in` and `lazy`, but attributes like `scope` and 
> `return` can be supported.

To be more precise, any attribute or storage class that is 
currently [specified as applying to the `this` parameter when 
used on a method][1] should be supported.

Personally, I would also **require** the `this` parameter of a 
`struct` method to be declared as `ref`, since [`this` is always 
passed by reference][2]. (An explicit `ref` is not needed for 
`class` methods, because classes are reference types.)

[1]: https://dlang.org/spec/class.html#member-functions
[2]: https://dlang.org/spec/expression.html#this


> - `this template parameter` can by replaced with explicit 
> `this` + normal template parameter.

I'm not sure allowing the user to specify the type of the `this` 
parameter is a great idea. Everything else in this proposal is 
just a new syntax for existing behavior; this would add entirely 
new behavior to the language.


More information about the dip.ideas mailing list