-
Notifications
You must be signed in to change notification settings - Fork 11
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
Type-assertion #749
Comments
FYI TypeScriptTS just like Nevalang doesn't have compile-time (complex) type information at runtime, so the only way to e.g. assume if something is of specific shape is by raw trying to access fields interface Cat {
name: string;
lives: number;
}
interface Dog {
name: string;
barks: boolean;
}
// Example data from a third-party library
const animals: (Cat | Dog)[] = [
{ name: 'Whiskers', lives: 9 }, // Cat
{ name: 'Fido', barks: true }, // Dog
];
// Type guard for Cat
function isCat(animal: any): animal is Cat {
return (animal as Cat).lives !== undefined;
}
// Type guard for Dog
function isDog(animal: any): animal is Dog {
return (animal as Dog).barks !== undefined;
}
// Function to handle animals
function handle(animal: Cat | Dog) {
if (isCat(animal)) {
console.log(`This is a cat named ${animal.name} with ${animal.lives} lives.`);
} else if (isDog(animal)) {
console.log(`This is a dog named ${animal.name} that barks: ${animal.barks}.`);
} else {
console.error("Unknown animal type");
}
}
// Example usage
for (const animal of animals) {
handle(animal);
} We either have to follow same approach or add Even then we won't have original (unresolved) forms of types of things |
Some dump from editor Solving problem of asserting a specific type on message of type
Parser/Sourcecode/Analyzer
Desugaring/IrgenRuntime
??? |
Related to #747 #726 #725
Problem (Structural Typing)
The thing about type-assertions in Nevalang is that messages doesn't hold their compile-time type information at runtime. In Go this problem is solved with nominative typing - values know names of their types. In Neva this isn't the case - message of type
User
is just some struct. Also, runtime data-types are different than compile-time, they are much simpler and doesn't have anything like type-parameters.I.e. there's no problem with asserting if
any
isbool, int, float or string
but with anything more complex there will be problems. It's not possible to assert thatv
islist<int>
, it's just a list. Same for structures. For enums it will probably be just int (maybe string if string-enums are a thing). For unions it's not possible at all, runtime doesn't know about their existence.The text was updated successfully, but these errors were encountered: