-
Notifications
You must be signed in to change notification settings - Fork 36
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
Lox Showcase #162
Lox Showcase #162
Conversation
@@ -0,0 +1,72 @@ | |||
export interface LoxMessage { | |||
type: LoxMessageType; | |||
content: unknown; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having unknown type is not so nice. There is a easy pattern I use:
type XType = 'A'|'B'|'C';
interface XBase {
type: XType;
}
interface A extends XBase {
type: 'A'; // subset of parent definition!
content: string;
}
interface B extends XBase {
type: 'B'; // subset of parent...
content: number;
}
interface C extends XBase {
type: 'C';
content: boolean;
}
type X = A|B|C;
//usage: you can do stuff like this:
const x: X = ... //get from somewhere
if(x.type === 'A') {
//it asserts then that x is of type A, so accessing x.content will be a string, no casts needed!!!
} else if(x.type === 'B') {
//...x.content is number
} else if(x.type === 'C') {
//...x.content is boolean
} else {
assertUnreachable(x.type); //this code is then unreachable (it will be highlighted red when another type D was added)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interpreter returns an unknown type, so I could do something like this:
export interface LoxMessage {
type: LoxMessageType;
content: MessageContent;
};
type MessageContent = string | number | boolean | null | any;
messages: TerminalMessage[]; | ||
} | ||
|
||
interface TerminalMessage { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the same trick as above to avoid Union type for content
@emilkrebs please squash your commits and rebase to the latest changes on the main branch: we've now switched deployment to GH Pages. |
2f78be6
to
57a150d
Compare
Two steps are needed first in order to publish this