Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot initialize const arrays with out parameters #18739

Open
dlangBugzillaToGithub opened this issue Dec 14, 2013 · 1 comment
Open

cannot initialize const arrays with out parameters #18739

dlangBugzillaToGithub opened this issue Dec 14, 2013 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Walter Bright (@WalterBright) reported this on 2013-12-14T11:08:45Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=11743

Description

The following shows the problem:
---------------------
struct S {
    const int[] x;
    const int[] y;
    const int   z;

    this(int i) {
        x = null;       // works
        foo(y);         // fails
        bar(z);         // works
    }
}

void foo(out int[] y) { y = null; }

void bar(out int z) { z = 1; }
---------------------

dmd -c foo
foo.d(9): Error: function foo.foo (out int[] y) is not callable using argument types (const(int[]))
@dlangBugzillaToGithub
Copy link
Author

k.hara.pg commented on 2013-12-14T22:00:57Z

(In reply to comment #0)
> The following shows the problem:
> ---------------------
> struct S {
>     const int[] x;
>     const int[] y;
>     const int   z;
> 
>     this(int i) {
>         x = null;       // works
>         foo(y);         // fails
>         bar(z);         // works
>     }
> }
> 
> void foo(out int[] y) { y = null; }
> 
> void bar(out int z) { z = 1; }
> ---------------------

I think bar(z); should also be rejected, because it would violate const system.

struct S
{
    immutable int z;

    this(int i)
    {
        bar(z); // works
    }
}

int* g;
void bar(out int z) { z = 1; g = &z; }

void main()
{
    S s = S(1);
    assert(s.z == 1 && is(typeof(s.z) == immutable int));
    *g = 100;
    assert(s.z == 1);   // fails!
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant