Static extension methods
_**Admin comment**: this is moving to implementation. [Feature specification](), [Implementation issue](https://github.com/dart-lang/sdk/issues/61484), Experiment flag: `static-extensions`._ ## Motivation Currently, extension methods do not support adding static methods/factory constructors. But this is a missed opportunity! There are many situations where semantically we want a static method/factory, but since the type is defined from an external source, we can't. For example, we may want to deserialize a `String` into a `Duration`. Ideally, we'd want: ```dart extension ParseDuration on Duration { factory parse(String str) => ...; } Duration myDuration = Duration.parse('seconds: 0'); ``` But that is currently not supported. Instead, we have to write: ```dart Duration parseDuration(String str) => ...; Duration myDuration = parseDuration('seconds: 0'); ``` This is not ideal for the same reasons that motivated extension methods. We loose both in discoverability and readability. ## Proposal The idea is to allow `static` and `factory` keyword inside extensions. ### Factory Factories would be able to capture the generic type of the extended type, such that we can write: ```dart extension Fibonacci on List<int> { factory fibonacci(int depth) { return [0, 1, 1, 2, 3, 5]; } } ``` Which means we'd be able to do: ```dart List<int>.fibonacci(6); ``` But not: ```dart List<String>.fibonacci(6); ``` Factories would work on functions and typedefs too (especial
Posted by rrousselGit on Dec 6, 2019, 8:20 AM
Tags
Related issues
- Add 65536 size support for maps
- TypeScript 7.1 Iteration Plan
- nvim-treeconfig: upstream nvim-treesitter
- Add the "action" and "method" attributes to <button>
- [FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
- > We were imposed GitHub for so many exercises!
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
support (Software)
Add 65536 size support for maps
TypeScript 7.1 Iteration Plan
nvim-treeconfig: upstream nvim-treesitter
Add the "action" and "method" attributes to <button>
[FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
> We were imposed GitHub for so many exercises!
Add support for Visual Studio 2026 Integration
Support for Helm v4
Static extension methods
_**Admin comment**: this is moving to implementation. [Feature specification](), [Implementation issue](https://github.com/dart-lang/sdk/issues/61484), Experiment flag: `static-extensions`._ ## Motivation Currently, extension methods do not support adding static methods/factory constructors. But this is a missed opportunity! There are many situations where semantically we want a static method/factory, but since the type is defined from an external source, we can't. For example, we may want to deserialize a `String` into a `Duration`. Ideally, we'd want: ```dart extension ParseDuration on Duration { factory parse(String str) => ...; } Duration myDuration = Duration.parse('seconds: 0'); ``` But that is currently not supported. Instead, we have to write: ```dart Duration parseDuration(String str) => ...; Duration myDuration = parseDuration('seconds: 0'); ``` This is not ideal for the same reasons that motivated extension methods. We loose both in discoverability and readability. ## Proposal The idea is to allow `static` and `factory` keyword inside extensions. ### Factory Factories would be able to capture the generic type of the extended type, such that we can write: ```dart extension Fibonacci on List<int> { factory fibonacci(int depth) { return [0, 1, 1, 2, 3, 5]; } } ``` Which means we'd be able to do: ```dart List<int>.fibonacci(6); ``` But not: ```dart List<String>.fibonacci(6); ``` Factories would work on functions and typedefs too (especial
Comments (0)
No comments yet. Start the discussion.