Skip to content

Using the method call to assign a value. #4704

Description

@simphotonics

Currently, the Object method call can be used like a getter with the option of providing several parameters.

Would it be feasible to allow using call as a generalised setter (for non-function objects)?

Use case: Consider an object a, that represents a matrix (see below):

final a = Matrix<int>(...);

The method call provides easy access to a certain matrix element:

final element = a(3,5);

To assign a value to the element, I currently use:

a.assign(3,5) = 42;

Would it be possible to add support for assigning a value using a similar syntax like the call function?

a(3,5) = 42;

/// A data structure that stores data of type [T]
/// in a 2-dimensional array.
/// Matrix elements can be iterated in row-major order.
class Matrix<T> extends Iterable<T> {
  final List<T> _data;
  int _columns;
  int _rows;

  /// Number of columns.
  int get columns => _columns;

  /// The number of rows.
  int get rows => _rows;

  ... 

  /// Returns the matrix element with row index [i] and column index [j].
  /// Throws a [RangeError] if [i] or [j] are out of range.
  T call(int i, int j) {
    _checkRowIndex(i);
    _checkColumnIndex(j);
    return _data[columns * i + j];
  }

  /// Assign [value] to the matrix element with row index [i] and column index [j].
  /// Throws a [RangeError] if [i] or [j] are out of range.
  void assign(int i, int j, T value) {
    _checkRowIndex(i);
    _checkColumnIndex(j);
    _data[columns * i + j] = value;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions