Skip to content

Add a TODO function that can be assigned to all types. #60582

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

Open
rensPols opened this issue Apr 16, 2025 · 8 comments
Open

Add a TODO function that can be assigned to all types. #60582

rensPols opened this issue Apr 16, 2025 · 8 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@rensPols
Copy link

It would be great to be able to pass a todo() function as an argument to make the compiler happy while you're testing/working on something else.
Kotlin already has such a function.

I've created my own function that does this, but since it is not native, it is not integrated with any IDE features concerning the TODO's in code.

// Optional reason
/// This function can be placed on a variable or parameter whose value is yet to be implemented.
Never TODO({String? reason}) {
  throw UnimplementedError(
    reason ?? 'TODO: Implementation missing (an empty todo() has been called)',
  );
}

// Mandatory reason
/// This function can be placed on a variable or parameter whose value is yet to be implemented.
Never TODO(String reason) {
  throw UnimplementedError(
    reason,
  );
}

I don't have a strong opinion on whether the reason should be optional or mandatory, but I would prefer the mandatory variant.

Usage

if (snapshot.hasError) {
  return Center(
    child: TODO("Display the error")
  );
}
var data = getData(
  "[type]",
  TODO("get the data range that has to be collected"),
  [
    Filter([..]),
    Filter([..]),
  ]
);

I hope this can be implemented!

@rrousselGit
Copy link

since it is not native, it is not integrated with any IDE features concerning the TODO's in code.

There are IDE extensions floating around to make custom TODO patterns.

You could flag TODO(.+?) as a a todo, and that'd work

@mmcdon20
Copy link

Dart has support for TODO comments with IDE integration.

Image

@rrousselGit
Copy link

I'm pretty sure they know about that already.
This is about reducing the boilerplate for both // TODO + throw Unimplemented

@FMorschel
Copy link
Contributor

We could also add optional parameters like person or something to better represent Flutter style TODOs. I like this idea.

@rensPols
Copy link
Author

@rrousselGit Thank you for pointing out the IDE extensions; I'll look into them. However, I would still very much prefer this function to be in the standard Dart SDK, because now I have to ensure that every developer in the project is using it. Also, another problem that occurs now is that if this function is used, currently, the Dart compiler/linter marks everything after that as unreachable, which it is, but it shouldn't be marked like that if it is a TODO.

To continue on @FMorschel, I think it would be a good idea indeed to add everything in the flutter_style_todos in the future. This would include:

  • message
  • username
  • issue URL

But even without the username and issue URL, having this function in Dart would still be incredibly pleasant. I think we should first focus on having that inside Dart before we talk about adding Flutter-specific functionality.

Because if we take the flutter_style_todos into account, the function signature needs to change depending on whether the flutter_style_todos linter is on or not. This would delay having this function a lot.

@FMorschel
Copy link
Contributor

Because if we take the flutter_style_todos into account, the function signature needs to change depending on whether the flutter_style_todos linter is on or not. This would delay having this function a lot.

I'd like to disagree here. We can make this lint trigger here if the TODO invocation doesn't have all of the required parameters.

@lrhn
Copy link
Member

lrhn commented Apr 19, 2025

It's not so much a function that can be assigned to any type, but one whose return type can be used in any context.

Even if the version of a TODO function above was in dart:core, it would still make everything after its call unreachable, that's just what Never means.
It might be possible to suppress the warning message, but the flow control has to treat it as returning Never.

Another option would be:

@Deprecated("Implement this")
// ignore: non_constant_identifier_names
R TODO<R>([String? message = "TODO"]) {
  throw UnimplementedError(message);
}

This function should adapt its return type to the context's expectation, and the Deprecated annotation will give you a warning everywhere you use it. That alone should give the behavior that is being asked for.
(If we had another annotation which meant "Don't have this in production code"

I don't know if this is a pattern that we'll want to endorse to the point where we add it to the platform libraries. It's sort-of nice, but I'd probably just write throw UnimplementedError("message") directly.

(Or use @doNotSubmit from package:meta instead of @Deprecated(...), it should still ensure that a real implementation is added before the code is considered complete.)

The case of "TODO" is obviously not lower-camel-case. Since it's intended to stand out, that could be argued to be working as intended. It could also be todo, but then it loses the visual link to the comment syntax.

@lrhn
Copy link
Member

lrhn commented Apr 19, 2025

We could also add optional parameters like person or something to better represent Flutter style TODO

Sounds like a function that Flutter should be adding to their platform libraries, not one belonging on the Dart SDK, which does not have any recommendations for todos.

If the Flutter recommendations for TODOs change, they would be in a position to update the function.

On the other hand, if Flutter wants a different TODO function than the plain Dart SDK, maybe the function is not really generally applicable. That's an argument against adding a function to the Dart platform libraries.
The Flutter SDK could modify the function, giving it more parameters than the Dart SDK's version, but then code using it couldn't be run on both. Or Flutter could have another TODO function in their own package, say package:flutter/widgets.dart or something else which is commonly imported. That would take precedence over one in dart:core in name resolution, but you can still use a TODO from dart:core if you really want to.

@lrhn lrhn transferred this issue from dart-lang/language Apr 22, 2025
@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug labels Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants