`this` template params for struct not expressing constness.

adnan338 relay.dev.adnan at protonmail.com
Mon Jun 8 09:08:40 UTC 2020


On Monday, 8 June 2020 at 08:10:19 UTC, Simen Kjærås wrote:
> On Monday, 8 June 2020 at 07:35:12 UTC, adnan338 wrote:
>>     Self* searchTree(this Self)(auto in ref T item) const {
>>         if (&this is null)
>>             return null;
>>         if (this.item == item)
>>             return &this;
>>         return (this.item < item) ?
>>             this.right.searchTree(item) :
>>             this.right.searchTree(item);
>>     }
>
> This method is const, which means 'this' is const, while Self 
> is not. What you're looking for here is inout 
> (https://dlang.org/spec/function.html#inout-functions):
>
>     auto searchTree()(auto in ref T item) inout {
>         if (&this is null)
>             return null;
>         if (this.item == item)
>             return &this;
>         return (this.item < item) ?
>             this.right.searchTree(item) :
>             this.right.searchTree(item);
>     }
>
> --
>   Simen

Thank you. Few followup questions, if you don't mind.

1. What does that blank template parameter mean?
2. Since `inout` acts as a wildcard for immutable/const/non-const 
qualifiers, what should I do to have the compiler ensure that my 
method does not mutate a non-const tree inside the body?



More information about the Digitalmars-d-learn mailing list