this post was submitted on 24 Nov 2025
20 points (100.0% liked)

Programming

23594 readers
119 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

Wrote this to reduce boilerplate when calling a function with a nullable parameter. Language is Dart. I like how concise it is but it feels weird to override []. Would you be okay with this?

extension CallMaybe<R, T> on R Function(T t) {  
  R? callMaybe(T? t) => switch (t) {  
    null => null,  
    T t => this(t),  
  };  

  R? operator [](T? t) => callMaybe(t);  
}  

void example() {  
  int? n;  
  math.sqrt[n];  
}  
you are viewing a single comment's thread
view the rest of the comments
[โ€“] 6nk06@sh.itjust.works 11 points 2 days ago (1 children)

I wouldn't use this and most people will forget that it exists. Most of the time you have do use the language and avoid hacks like that.

Don't you have alternatives in Dart?

[โ€“] Lojcs@piefed.social 3 points 2 days ago

I don't think so, the null aware operator doesn't rescue the nullable out of the parameter list.