-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Guarded function declarations #9253
Comments
If |
Yes this was my original intention.
I'm not sure I understand you. Why would the caller need If the functions is being passed as a callback corns still needs to be checked that it is not null. let corns: string | null;
function makePopCorn(): string
requires corns !== null
{
return 'pop';
}
function call(callback: any) {
callback();
}
if (corns) {
call(makePopCorn);
}
call(makePopCorn); // error corns can be null |
But that has a different meaning. It means don't allow let corns: string | null;
function makePopCorn(): string
requires corns !== null
{
return 'pop';
}
function callLater(callback: any) {
setTimeout(() => callback(), 1000);
}
if (corns) {
callLater(makePopCorn);
}
corns = null; |
Yes I know. Your example have been covered before in the topic of callbacks in |
OK but then why not just check inside the function? The questions of immediacy, how the function is called and by whom all become irrelevant if you check inside the function itself. It seems like a very strange way to partially guarantee something that can be guaranteed by checking at the time of the call. Maybe I need to see a more comprehensive example, but it seems odd to have a function that has a precondition which is not related to any argument, or state used by the function and which cannot be checked when it is passed to a separate lexical scope. Also, I believe the issue with |
I write a lot of functions and private methods that expects some referenced variables or properties to be not null. I can use the unwrap operator to unwrap them or just add an if statement in those cases. The latter paying a runtime cost and the former paying an unsafe cost. I think declaring the function to expect some references to be not null is the most type safest and you also don't pay a runtime cost with one or more if checks. The function declarations are also more readable function ... requires something !== null. An example case given below. Model can be null. bindModel method is declared below that class PostView() {
model: Post | null;
view() {
}
bind() {
if (this.post) {
this.bindModel();
}
}
bindModel(): void
requires this.model != null
{
this.model.on('change:title', () => alert('hello'))
}
} |
OK, I see what you are saying, but now that you can use bindModel(model: Post): void {
model.on('change:title', () => alert('hello'));
} gives you what you want without adding additional syntactic complexity to the language. This also makes the function more reusable, readable, and explicit about its dependencies. It says |
I haven't really liked parameterisation solutions. It can get quite ugly if you have a lot of parameters you need to define. Though I agree it solves the problem. |
how can you ensure anything via checks at runtime? |
This seems way too complex for something that could really only represent a small fraction of the kind of "I need X side effects first" things you'd really need to make this a comprehensive solution. |
I sometimes reference outer scope variables and class members in my function/method declarations. The problem is that those references are nullables. I want a way to ensure that a function is called on a call site where those references are not null.
Related #198. But not duplicate.
The text was updated successfully, but these errors were encountered: