-
Notifications
You must be signed in to change notification settings - Fork 68
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
When determining implemented interfaces in union types langium should honor inheritance hierarchy #1544
Comments
Hey @m-novikov, I know it can be a little confusing, but this is working as designed. Please do not mix & match inferred and declared data types within the same inheritance structure. This doesn't work, as the inference mechanism infers different types than whatever is declared as your types. This leads to a mismatch down in the This modified grammar will work instead: grammar HelloWorld
entry Model:
(entities+=Entity | receptors+=Receptor)*;
interface Base {}
interface Person extends Base {name: string}
interface Receptor {in: Base}
Person returns Person:
'person' name=ID;
interface Bot extends Base {id: string}
Bot returns Bot:
'id' id=ID;
type Entity = Bot | Person;
Entity returns Entity: Bot | Person;
Receptor returns Receptor:
'receptor' in=Entity;
hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;
hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/; We might be able to improve on this, but the type system is already hugely complex as it is, so I'm not sure it's actually feasible. I'll keep this issue open for now. |
My original use case is to avoid forward declaring all the types, to have them in other file as partial interfaces.
But then resulting AST has following export interface Entity extends AstNode {
readonly $type: 'Bot' | 'Entity' | 'Person';
} Which is incorrect, because "Entity" would be never instantiated and my goal to enforce that switch/case is exhaustive using typescript when applied to Entity union type. |
I guess I can see that, but why not just use |
While introducing cycling dependencies does work, thank you for that. It has it's own problems due to the fact that it not only imports types, but also all the other rules from the grammar. |
Right, but that's really only problematic for hidden terminal rules - everything else gets pulled into the grammar in an on-demand basis. If you don't use an imported grammar rule, it won't be in your final, generated grammar. |
Langium version: 3.0, 3.1
Package name: langium
Steps To Reproduce
The current behavior
Compilation fails with message:
The expected behavior
Compilation takes into account that interface Person also extends Base and compilation completes.
The text was updated successfully, but these errors were encountered: