November 14

On Tuesday, 14 November 2023 at 16:39:42 UTC, dhs wrote:

>

Does this "const S2" then get implicitly converted to "S2", before being assigned to "ss2"? I tried adding an opAssign() to S2 - it's not getting called. So I'm not sure what is actually going on here.

There's no assignment. The value is constructed in-place, in ss2's memory.

The reason the compiler allows you to construct a const(S2) value inside of an S2 variable is that const(S2) implicitly converts to S2.

November 14

On Tuesday, 14 November 2023 at 13:58:17 UTC, Paul Backus wrote:

>

It's easier to see if you compare the actual and expected argument lists side-by-side

Expected: (ref const(S1) s) const
Actual:   (    const(S1)  )
                            ^^^^^
                 Mismatched 'this' argument

This would be a much better output. Is this something you made up or did you get it from one of the compilers? LDC2, which is what I tested with, reported in the format that I mentioned.

It might be something to add to the compiler that mismatches in this qualifiers should be reported separately, especially since the mutable form has no explicit qualifier. But I guess this is only an issue for constructors, because normal const functions can be called with mutable objects.

That being said, I still consider this a bug, if the inout version works, the const version should work as well. I don't see the difference.

So an interesting thing, if I change the int i to int *i in S2, instead of compiling I get the error:

Error: `inout` constructor `testinoutctor.S2.this` creates const object, not mutable

Which gives a much nicer error message!

-Steve

November 14

On Tuesday, 14 November 2023 at 16:51:07 UTC, Paul Backus wrote:

>

There's no assignment. The value is constructed in-place, in ss2's memory.

The reason the compiler allows you to construct a const(S2) value inside of an S2 variable is that const(S2) implicitly converts to S2.

On Tuesday, 14 November 2023 at 16:58:25 UTC, Steven Schveighoffer wrote:

>

That being said, I still consider this a bug, if the inout version works, the const version should work as well. I don't see the difference.

Ok so to summarize:

  • "inout" and "const" behaving differently here is probably wrong, and
  • in structs without reference fields, a const constructor can be used to construct mutable values, due to "implicit qualifier conversions".

Thanks.

November 16

On Tuesday, 14 November 2023 at 13:58:17 UTC, Paul Backus wrote:

>

On Tuesday, 14 November 2023 at 13:41:32 UTC, Steven Schveighoffer wrote:

>
Error: copy constructor `testinoutctor.S1.this(ref const(S1) s) const` is not callable using argument types `(const(S1))`

I'm not sure what this means. There shouldn't be a copy being made here, as the thing is already const. I don't understand this error, and it looks like a bug to me.

The error is saying that the copy constructor expects a const this argument, but you're passing a mutable this argument.

Thanks for explaining, it's a confusing error.

The error when the constructor is changed to be immutable is:

Error: `immutable` method `immutable_ctor.S1.this` is not callable using a mutable object

(I've made a fix to say constructor instead of method for that.)
Now I've made a fix to produce a similar error for const:
https://issues.dlang.org/show_bug.cgi?id=24248

1 2
Next ›   Last »