November 26
On Sat, Nov 27, 2021 at 03:41:05AM +0000, Paul Backus via Digitalmars-d wrote: [...]
> For example, here's a really common error that beginning D programmers make:
> 
> ```d
> struct S
> {
>     private int[] a;
>     const int[] getArray() const { return a; }
> }
> ```
> 
> If you try to compile this code, you get the following error:
> 
> ```
> Error: redundant attribute `const`
> ```
> 
> The reason for this is that in the above code, *both* instances of `const` are treated as applying to the `this` reference, not to the return value.  The fix is to add `()` to the first `const`:
[...]

IMO, const as a type qualifier should *always* be used with parentheses. Things like `const int` are a code smell, and an invitation to trouble like the above.  This is D, not C, so we should be writing things the D way, i.e., `const(int)` instead of `const int` like we do in C/C++.

Another case is the subtle but important difference between `const(X)[]` and `const(X[])`.  When you write `const X[]` it's not immediately obvious which one you get.  This is also a cause of stumbling for D beginners.

Therefore, IMNSHO it's a misfeature that the D compiler accepts `const int`. It should reject such cases and always require parentheses, then there would be no ambiguity.  Had parentheses been mandatory from the beginning, people would have learned to write const(X)[] and const(X[]) unambiguously, and this would not have been a problem at all. Optional parentheses is a source of needless trouble.

Ditto with immutable, inout, and any other type constructor.


T

-- 
A mathematician learns more and more about less and less, until he knows everything about nothing; whereas a philospher learns less and less about more and more, until he knows nothing about everything.
November 27

On Saturday, 27 November 2021 at 04:20:57 UTC, max haughton wrote:

>

On Saturday, 27 November 2021 at 03:41:05 UTC, Paul Backus wrote:

>

On Saturday, 27 November 2021 at 01:17:15 UTC, zjh wrote:

>

[...]

Actually, I got the idea from Python. :)

Why do I think it's a good idea? Because it makes it really obvious which attributes apply to this and which attributes apply to the function as a whole.

[...]

I have been mumbling to myself about this for a while. I think it's a fundamentally better way of expressing the attribute soup.

Can we actually switch? Not sure...

I don't know about "switch", but we can certainly add the new syntax as an optional alternative to the current version. Most likely the original syntax will never entirely go away, though, just like the original alias old new; syntax, so the real question is whether having both is better than just having the current version.

November 27

On Saturday, 27 November 2021 at 03:41:05 UTC, Paul Backus wrote:

>
struct S
{
    private int[] a;
    const int[] getArray() const { return a; }
}

We should collect a beginner's error prone items,And put a link on the home page.
can we like this?

@constthis
const int[] getArray() { return a; }

python's self is everywhere,it's very annoying.

November 27

On Saturday, 27 November 2021 at 04:31:31 UTC, H. S. Teoh wrote:

>

write const(X)[] and const(X[]) unambiguously, and this would not have been a problem at all. Optional parentheses is a source of needless trouble.

Ditto with immutable, inout, and any other type constructor.

Your post has many good ideas and should be collected.

November 27

On Saturday, 27 November 2021 at 01:10:37 UTC, zjh wrote:

>

C++ references, which look complicated, are actually simple.

Try reading the C++ spec, you will see the rules are far more complicated than D ref.

>

And D ref, no matter what, I hate it.

Not helpful without giving a reason.

>

A '&' is Ok,why not use it?

Because:

  1. ref is not part of the type, so it is used less often and we don't need a sigil for it. It should stand out, it's important.
  2. & is already an operator, it's best not to overload sigils.
  3. C++ & (as a type constructor) is different from D ref semantically, so we should not use the same syntax to avoid introducing bugs when porting code from C++.
>

There are too many 'attributes', can we delete? I'm writing functions, not attrs.

If we are to support checked memory safety we need more attributes than C++, which doesn't support it. In general attributes could be inferred when the function body is there, but I think that is only implemented for templates ATM. Perhaps it would impact compile-times.

November 27

On Saturday, 27 November 2021 at 03:41:05 UTC, Paul Backus wrote:

>

For example, here's a really common error that beginning D programmers make:

struct S
{
    private int[] a;
    const int[] getArray() const { return a; }
}

...

>

With an explicit this parameter, there is no room for ambiguity:

    const int[] getArray(const ref this) { return a; }

The outer const can only apply to the return value, and the inner const can only apply to the this reference.

Explicit this parameter is OK but more verbose. It also could be confusing for learners if declaring the this parameter is not required for methods with no this attributes. And if it is required, would that break existing code? I suppose there could be a rule that all methods of a type must follow the same convention.

Another solution is to require attributes that apply to this to go after the parameters, not before. That would be less verbose, but may break code and so would need a long deprecation cycle. (This was first suggested some years ago).

November 27

On Saturday, 27 November 2021 at 11:10:42 UTC, Nick Treleaven wrote:

>

it would impact compile-times.

compile-times is faster than manual write.

November 27

consistency!

we have int* for pointers

let's use int& for references

consistency > everything else, i don't use ref because i hate typing it, same for immutable

November 27

On Saturday, 27 November 2021 at 14:35:17 UTC, russhy wrote:

>

consistency!
we have int* for pointers
let's use int& for references
consistency > everything else, i don't use ref because i hate typing it, same for immutable

D's main selling point is elegance.
Sometimes, I think C++ is more elegant than d. It's totally incredible.
We need to slash something and simplify something.
No programmer don't want to be lazy. No one wants to type more.the same for the libwriter.

November 27

On Saturday, 27 November 2021 at 14:35:17 UTC, russhy wrote:

>

consistency > everything else

Storage classes in D consistently use keywords, making it look similar to int* (which is a type, not a storage class) would be inconsistent.

>

i don't use ref because i hate typing it, same for immutable

That sounds more like conciseness, not consistency.

1 2 3 4 5 6 7 8