Dan R
Dan R

Reputation: 1361

How can I create a typedef representing a Dart record with named fields?

I would like to define a typedef representing a Dart record with named fields but am not sure what the syntax would be.

The code below shows how to define the typedef Command representing a record with two positional fields:

/// A record holding:
/// * the name of an executable,
/// * the list of arguments.
typedef Command =(
  String executable,
  List<String> arguments,
);

extension Compose on Command {
  /// Joins the executable and the arguments.
  String get cmd => '${this.$1} ${this.$2.join(' ')}';
}

Upvotes: 7

Views: 1540

Answers (1)

offworldwelcome
offworldwelcome

Reputation: 1688

Just add the named parameter list:

/// A record holding:
/// * the name of an executable,
/// * the list of arguments.
typedef Command = ({ // <== named parameter list begin
  String executable,
  List<String> arguments,
});

extension Compose on Command {
  /// Joins the executable and the arguments.
  String get cmd => '${this.executable} ${this.arguments.join(' ')}';
}

Upvotes: 14

Related Questions