Can someone tell me what the compiler thought I was trying to do?

Mike Parker aldacron at gmail.com
Sat Oct 15 00:43:04 UTC 2022


On Friday, 14 October 2022 at 21:51:54 UTC, WhatMeWorry wrote:
>
> I lost about a half an hour troubleshooting some code of mine 
> which as it turned out to be resolved with just one line.
>
>
> // paths.remove(i);   // compiles fine but does nothing
>
> paths = paths.remove(i);  // works - what I erroneously thought 
> the previous line was doing
>
> Is the first line nonsensical and should the compiler have at 
> least issued a warning?

At the moment, no. You should have read the documentation of the 
function :-)

> Note that remove does not change the length of the original 
> range directly; instead, it returns the shortened range. If its 
> return value is not assigned to the original range, the 
> original range will retain its original length, though its 
> contents will have changed:

You ignored the return value of a function you shouldn't have 
ignored. It's not practical for the compiler to warn every time 
you do that, as it currently can't know that you're *supposed* to 
use it.

`@mustuse` was added to the language in 2.100.0 as an attribute 
for `struct` or `union`, but not yet for functions, as explained 
in the DIP:

https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1038.md#mustuse-as-a-function-attribute

If that ever gets expanded for use as a function attribute, then 
it can be used in situations like this so that you must use the 
return result. Until then, read the documentation!


More information about the Digitalmars-d-learn mailing list