Skip to content

Add named factory constructors to Duration for single-unit creation #62796

@GuillaumeMCK

Description

@GuillaumeMCK

Motivation

Creating a Duration from a single time unit is a common operation. The current constructor handles it, but requires the reader to identify the one meaningful argument among six possible named parameters:

const timeout = Duration(seconds: 30);
const delay   = Duration(milliseconds: 500);

Named factory constructors would make the intent explicit at the call site:

const Duration timeout = .seconds(30);
const Duration delay   = .milliseconds(500);

This also improves IDE accessibility. Typing Duration. surfaces all available time units via autocomplete, which lowers the discovery barrier for developers unfamiliar with the full constructor signature.


The const problem with extensions

The popular 30.seconds pattern (via extension on int) is widely used in third-party packages but cannot be const. This makes it unsuitable for compile-time constants, annotation arguments, or any context where const is required.

Named factories on Duration itself have none of these limitations:

// extension on int — not const
final timeout = 30.seconds;

// named factory — fully const
const timeout = Duration.seconds(30);

Dot shorthand synergy

With Dart's dot shorthand syntax, named factories become especially concise when the type is inferred from context:

Future.delayed(.milliseconds(500), callback);
stream.timeout(.seconds(30));
Timer(.minutes(1), callback);

This pattern is readable, const-compatible, and fits naturally with the direction the language is already moving.


Proposed API

const factory Duration.days(int amount);
const factory Duration.hours(int amount);
const factory Duration.minutes(int amount);
const factory Duration.seconds(int amount);
const factory Duration.milliseconds(int amount);
const factory Duration.microseconds(int amount);

Each factory is a direct delegate to the existing primary constructor. No new behavior is introduced.


Usage examples

// compile-time constants
class Config {
  static const Duration sessionTimeout = .minutes(30);
  static const Duration retryDelay     = .seconds(5);
  static const Duration cacheTTL       = .hours(24);
}

// inline with dot shorthand
Future.delayed(.milliseconds(500), callback);
socket.timeout(.seconds(30));

Notes

This change is purely additive. No existing API is modified or removed. There is no runtime cost relative to the existing constructor. Implementation would only touch sdk/lib/core/duration.dart and its associated tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.feature-static-extensionsImplementation of the Static extensions featurelibrary-coretype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions