Skip to content

Constructors can't be used in higher order functions #46789

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

Closed
gaaclarke opened this issue Aug 2, 2021 · 2 comments
Closed

Constructors can't be used in higher order functions #46789

gaaclarke opened this issue Aug 2, 2021 · 2 comments

Comments

@gaaclarke
Copy link
Contributor

gaaclarke commented Aug 2, 2021

Description

I have to wrap constructors in factory methods in order to use them in higher-ordrer functions. I expect that in a context where a function is required that has the same signature as the constructor, a type name should be interpreted as the constructor.

Code

/// A [map] function that takes in 2 iterables.
Iterable<V> _map2<T, U, V>(
    Iterable<T> ts, Iterable<U> us, V Function(T t, U u) func) sync* {
  final Iterator<T> itt = ts.iterator;
  final Iterator<U> itu = us.iterator;
  bool uMoveNext = itu.moveNext();
  bool tMoveNext = itt.moveNext();
  while (uMoveNext && tMoveNext) {
    yield func(itt.current, itu.current);
    uMoveNext = itu.moveNext();
    tMoveNext = itt.moveNext();
  }
  if (itu.moveNext() || itt.moveNext()) {
    throw Exception('Arguments to _map2 aren\'t the same length.');
  }
}

class _Tuple<T, U> {
  _Tuple(this.first, this.second);
  static _Tuple<T, U> make<T, U>(T t, U u) => _Tuple<T, U>(t, u);
  T first;
  U second;
}

// WORKAROUND
Iterable<_Tuple<T, U>> _zip<T, U>(Iterable<T> ts, Iterable<U> us) =>
    _map2(ts, us, _Tuple.make);

// DOESN'T WORK
Iterable<_Tuple<T, U>> _zip<T, U>(Iterable<T> ts, Iterable<U> us) =>
    _map2(ts, us, _Tuple);
@lrhn
Copy link
Member

lrhn commented Aug 2, 2021

We're working on it. #46228

@gaaclarke
Copy link
Contributor Author

gaaclarke commented Aug 2, 2021

closing as a duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants