Jump to page: 1 2
Thread overview
Can someone tell me what the compiler thought I was trying to do?
Oct 14, 2022
WhatMeWorry
Oct 14, 2022
H. S. Teoh
Oct 15, 2022
Mike Parker
Oct 19, 2022
H. S. Teoh
Oct 19, 2022
rikki cattermole
Oct 19, 2022
Adam D Ruppe
Oct 19, 2022
H. S. Teoh
Oct 19, 2022
mw
Oct 19, 2022
Adam D Ruppe
Oct 19, 2022
mw
Oct 19, 2022
Paul Backus
Oct 19, 2022
mw
Oct 19, 2022
zjh
Oct 19, 2022
zjh
Oct 19, 2022
zjh
Oct 19, 2022
zjh
Oct 19, 2022
Mike Parker
Oct 19, 2022
Mike Parker
Oct 19, 2022
rikki cattermole
Oct 15, 2022
Mike Parker
October 14, 2022

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?

October 14, 2022
On Fri, Oct 14, 2022 at 09:51:54PM +0000, WhatMeWorry via Digitalmars-d-learn 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?

Depending on what your range type was, it may not necessarily do *nothing* (it may mutate the range).  But as Andrei put it, "range mutation functions change content, not topology". It *returns* the new "topology", i.e., the new range after the removal; so you need to assign it to the original range in order to keep it up-to-date.

Given that this particular trap crops up regularly, perhaps some sort of warning ought to be added. Once the @nodiscard DIP is accepted & implemented this should be easy to do.


T

-- 
"I'm running Windows '98." "Yes." "My computer isn't working now." "Yes, you already said that." -- User-Friendly
October 15, 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!

October 15, 2022

On Friday, 14 October 2022 at 22:17:52 UTC, H. S. Teoh wrote:

>

Given that this particular trap crops up regularly, perhaps some sort of warning ought to be added. Once the @nodiscard DIP is accepted & implemented this should be easy to do.

Seems like you're behind the times! The DIP was accepted and implemented with some changes:

https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1038.md#final-review

The summary in full:


The language maintainers accepted this DIP with a request for changes:

  • rename @noDiscard, as they want to avoid adding additional negative attributes to the language.
  • address issues that arise from the feature's interaction with inheritance when applied to classes.
  • develop rules for handling covariance and contravariance when applied to functions.

The DIP author addressed these requests by renaming the attribute to @mustuse and allowing it only on structs and unions. His rationale for the latter is described in the section, Design Goals and Possible Alternatives.

The maintainers approved the author's changes and accepted the revised version of the DIP.

October 18, 2022
On Sat, Oct 15, 2022 at 12:47:02AM +0000, Mike Parker via Digitalmars-d-learn wrote:
> On Friday, 14 October 2022 at 22:17:52 UTC, H. S. Teoh wrote:
> 
> > Given that this particular trap crops up regularly, perhaps some sort of warning ought to be added. Once the @nodiscard DIP is accepted & implemented this should be easy to do.
> > 
> 
> Seems like you're behind the times! The DIP was accepted and implemented with some changes:
> 
> https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1038.md#final-review

Has it really been implemented?  I tested the latest git master, the following code doesn't compile:

------
@mustUse int fun() { return 455; }

void main() {
	fun();
}
------

Compiler output:

------
/tmp/test.d(1): Error: undefined identifier `mustUse`
------

I tried @mustuse, @nodiscard, @noDiscard, no good.  What am I missing?


T

-- 
This is not a sentence.
October 19, 2022
https://github.com/dlang/dmd/blob/master/druntime/src/core/attribute.d#L292
October 19, 2022
On Wednesday, 19 October 2022 at 00:57:31 UTC, H. S. Teoh wrote:
> Has it really been implemented?  I tested the latest git master, the following code doesn't compile:

it only applies to types, not to functions.
October 18, 2022
On Wed, Oct 19, 2022 at 01:15:37AM +0000, Adam D Ruppe via Digitalmars-d-learn wrote:
> On Wednesday, 19 October 2022 at 00:57:31 UTC, H. S. Teoh wrote:
> > Has it really been implemented?  I tested the latest git master, the following code doesn't compile:
> 
> it only applies to types, not to functions.

Wat... so what's the use of it then?  So it's not possible to mark the return value of an int function @mustUse without making, in theory, *all* ints @mustUse?

I must confess I'm baffled as to the purpose of this strange design.


T

-- 
Doubt is a self-fulfilling prophecy.
October 19, 2022
On Wednesday, 19 October 2022 at 01:30:23 UTC, H. S. Teoh wrote:
> On Wed, Oct 19, 2022 at 01:15:37AM +0000, Adam D Ruppe via
>> 
>> it only applies to types, not to functions.
>
> Wat... so what's the use of it then?  So it's not possible to mark the return value of an int function @mustUse without making, in theory, *all* ints @mustUse?
>
> I must confess I'm baffled as to the purpose of this strange design.
>

Same, can't believe it.

Is there any (design) doc about this?

October 19, 2022
On 19/10/2022 2:30 PM, H. S. Teoh wrote:
> On Wed, Oct 19, 2022 at 01:15:37AM +0000, Adam D Ruppe via Digitalmars-d-learn wrote:
>> On Wednesday, 19 October 2022 at 00:57:31 UTC, H. S. Teoh wrote:
>>> Has it really been implemented?  I tested the latest git master, the
>>> following code doesn't compile:
>>
>> it only applies to types, not to functions.
> 
> Wat... so what's the use of it then?  So it's not possible to mark the
> return value of an int function @mustUse without making, in theory,
> *all* ints @mustUse?
> 
> I must confess I'm baffled as to the purpose of this strange design.

Oh but it gets better:

From C23 draft:

The nodiscard attribute shall be applied to the identifier in a function declaration or to the definition
of a structure, union, or enumeration type. If an attribute argument clause is present, it shall have
the form:
( string-literal )
« First   ‹ Prev
1 2