dscanner and ref parameters

Dennis dkorpel at gmail.com
Sun Feb 23 15:00:29 UTC 2020


On Sunday, 23 February 2020 at 09:03:56 UTC, mark wrote:
> Then this would not only help dscanner, but also make it clear 
> to programmers that the argument could be modified.
>
> (This is done in Rust with f(&mut arg), and I certainly find it 
> helpful.)

C# also does it, and uses exactly the same keyword as D:

> To use a ref parameter, both the method definition and the 
> calling method must explicitly use the ref keyword, as shown in 
> the following example.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

Making it mandatory in D would cause code breakage and hamper 
generic code, but like you suggest, making it optional is an 
interesting idea. It can also enforce that something is actually 
passed by ref to avoid accidental copies, which currently 
requires manual checking. For example in std.format:

> // force toString to take parameters by ref
> static assert(!__traits(compiles, val.toString(s, 
> FormatSpec!Char())));
> static assert(!__traits(compiles, val.toString(S(), f)));}

https://github.com/dlang/phobos/blob/fee0697a8328dc22258cf09753f39ad5984015db/std/format.d#L3887




More information about the Digitalmars-d-learn mailing list