-
Notifications
You must be signed in to change notification settings - Fork 205
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
Request: calling a function only when a parameter is not null. #360
Comments
I wish Dart's null were the Then you could just write
|
This could also be easily solved by macros, and the release of macros could include a macro for this. Rust style macro (I think):
|
But Rust can do macro expansion and analysis at compile time. That is not a good approach for a language running on a virtual machine. Also, Dart's appeal stems from its simplicity. Complex meta programming is the opposite of simplistic. Only a hand full of Rust gurus even touch macros.
And that gets unwieldy pretty quickly. I have written my Rust. While it might be sensible to have Options and Results in a systems programming language, on a higher level they are clutter. And in the real world you only have to perform few null checks. But to your original problem: With the null aware operator you could just wirte |
Sadly A shorter Another option is to make an operator which only applies to arguments, and which short-circuit the surrounding call if the value is null, say:
So, I'd prefer a proper delimited |
Dart already has compile-time constants. I don't think that proves that macros wouldn't have problems, but, I think many people would have made the same arguments against
I think ideally we have a handful of gurus making simple macros. But you're right, once we release something, its hard to know how it will be abused. |
Any solution we have here that's syntax based will likely get really soupy really quick (even Named operations are much better as the number of operations increases. If we want to do names, a la
then we either have to add macros or built-ins like One more thought, if we add pattern matching, then it might be best to do (no idea on proposed syntaxes)
This would be soupy but at least lean on existing concepts. |
Just a quick plug that extension methods will allow you to write this (well, except for the implicit parameter lambda): extension Exists<S, T> on T? {
S? exists(S Function(T) f) => (this == null) ? null : f(this);
}
void test() {
toPrint.exists(print); // calls print on toPrint iff toPrint is non-null
} |
that is fantastic! |
Even without extensions, I think you could do: R applyIfExists<R, T>(R Function(T) f, T arg) => (arg == null) ? null : f(arg); which I personally think is more readable/familiar than |
Another extension method option is to extend a unary function: extension Exists<T, R> on R Function(T) {
R? ifArg(T? argument) => argument == null ? null : this(argument);
}
void test(Object? toPrint) {
print.ifArg(toPrint);
} |
In Dart
|
extension Exists<S, T> on T? {
S? exists(S Function(T) f) => switch(this) {
null => null,
T self => f(self),
};
} |
Related to #190 |
Pipe operator would help here: |
It's awkward to call a function when a parameter is not null, such as:
This is significantly more work than calling a method when the target is not null:
So naturally we developers have gotten privileged and want both :)
The text was updated successfully, but these errors were encountered: