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

Make the $container type more precise in case of guard conditions #1300

Open
JohannesMeierSE opened this issue Nov 22, 2023 · 2 comments
Open
Labels
grammar Grammar language related issue types Types related issue

Comments

@JohannesMeierSE
Copy link
Contributor

The following simple and artificial example uses guard conditions in order to switch between different cases (excerpt):

Teacher:
    'teacher' name=ID values=Reused<true>;

Student:
    'student' name=ID values=Reused<false>;

Reused<condition>:
    <!condition> OneValue | <condition> TwoValues;

OneValue:
    'values' value=ID;
TwoValues:
    'values' valueOne=ID valueTwo=ID;

(The whole grammar with an instance can be found in the playground.)

For this grammar, Langium generates the following types in the ast.ts (excerpt):

export type Reused = OneValue | TwoValues;

export interface OneValue extends AstNode {
    readonly $container: Student | Teacher;
    readonly $type: 'OneValue';
    value: string
}

export interface TwoValues extends AstNode {
    readonly $container: Student | Teacher;
    readonly $type: 'TwoValues';
    valueOne: string
    valueTwo: string
}

From my point of view, the types of the $container properties could be more precise, i.e. only Student for OneValue and only Teacher for TwoValues.
The current $container types are not wrong, but as a user of Langium, I would expect the following types, since the grammar clearly shows, that these types are enough:

export type Reused = OneValue | TwoValues;

export interface OneValue extends AstNode {
    readonly $container: Student;
    readonly $type: 'OneValue';
    value: string
}

export interface TwoValues extends AstNode {
    readonly $container: Teacher;
    readonly $type: 'TwoValues';
    valueOne: string
    valueTwo: string
}
@JohannesMeierSE
Copy link
Contributor Author

JohannesMeierSE commented Nov 22, 2023

If you think this issue is worth an improvement, I am willing to work on this issue!

@spoenemann
Copy link
Contributor

Yes for such clear cases it would make sense to restrict the container type.

@spoenemann spoenemann added grammar Grammar language related issue types Types related issue labels Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
grammar Grammar language related issue types Types related issue
Projects
None yet
Development

No branches or pull requests

2 participants