On Monday, 19 November 2018 at 02:08:14 UTC, Dennis wrote:
On Monday, 19 November 2018 at 01:24:02 UTC, Stanislav Blinov wrote:
Yup, that's because, like Rubn said, copying value types is trivial. Where it all comes to bite you is when you start having pointers, because you can't copy a const(T)* into a T*.

I'm not using reference types, but still:

```
struct S {
    int a;
    this(int a) {
        this.a = a;
    }
}

void main()
{
    immutable S d = 3;
}

```

onlineapp.d(10): Error: mutable method onlineapp.S.this is not callable using a immutable object onlineapp.d(10): Consider adding const or inout to onlineapp.S.this

const still leaves the first error...

You're skimming the examples ;)

struct S {
    int a;
    this(int a) { this.a = a; }
    this(int a) const { this.a = a; }
    this(int a) immutable { this.a = a; }
}

or

struct S {
    int a;
    this(this T)(int a) { this.a = a; }
}

...inout works though I don't know what it does.

Recall that member functions (including constructors) are just functions in disguise:

struct S {
    this(int a) inout { /* ... */ }
}

what that boils down to, conceptually, is:

void _compiler_made_it_a_struct_S_constructor(ref inout S this, int a);

In other words, an `inout` method makes the "this" reference an `inout`. Same goes for const, immutable, etc.

Reply via email to