Issue 24345 - Issue with `alias this = xyz` inside structs.
Summary: Issue with `alias this = xyz` inside structs.
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 enhancement
Assignee: No Owner
URL: http://dlang.org/
Keywords:
Depends on:
Blocks:
 
Reported: 2024-01-19 06:55 UTC by Danilo
Modified: 2024-02-07 13:13 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Danilo 2024-01-19 06:55:38 UTC
Example code:

```d
module app;
import std;

void main() {
    auto x = new Derived(10, 20);
}

struct Base {
    int a;
}

struct Derived
{
    Base base;
    int b;
    alias this = base;

    @disable this();

    // `a` is not available for use with constructor parameters.
    //
    this( typeof(a) _a, typeof(b) _b ) {
        //
        // inside the body of the constructor, `a` is available!
        //
        a = _a;
        b = _b;

        writeln( a, ", ", b );
    }
}
```

Error:
`Error: undefined identifier a`

Problem:
The compiler can't find the symbol `a` in the constructor parameters, although the symbol was "imported" into the struct scope by using `alias this = base;`.
Comment 1 timon.gehr 2024-01-27 02:44:36 UTC
This works with `-preview=fixAliasThis`. Maybe we can close this as WORKSFORME?
Comment 2 Danilo 2024-01-30 06:38:50 UTC
(In reply to timon.gehr from comment #1)
> This works with `-preview=fixAliasThis`. Maybe we can close this as
> WORKSFORME?

Thanks, that works!

Will the preview get included into regular D in the near feature?
„WorksForMeOnly“ is not so good if you want to make a public D package. ;)