Skip to content

Better support for shared modular behaviors. #3169

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

Open
modulovalue opened this issue Jun 28, 2023 · 0 comments
Open

Better support for shared modular behaviors. #3169

modulovalue opened this issue Jun 28, 2023 · 0 comments

Comments

@modulovalue
Copy link

modulovalue commented Jun 28, 2023

Currently, it is inconvenient to declare a set of declarations with a set of shared and configurable behaviors.

Consider, for example, a set of declarations that all need to have access to a shared value or type parameter.

Today, one needs to use abstract factories where everything is propagated explicitly. Below is a simple example of an abstract factory that consists of just a single implementation:

void main() {
  final moduleA = MyFactory<Node>(myNodeEq, 0);
  moduleA.fooFactory();
  final moduleB = MyFactory<int>(myIntEq, 1);
}

class MyFactory<T> {
  final Equality<T> eq;
  final int constant;
  
  const MyFactory(this.eq, this.constant);

  Foo<T> fooFactory() => Foo<T>(eq, constant);

  Bar<T> barFactory() => Bar<T>(eq, constant);
}

class Foo<T> {
  final Equality<T> eq;
  final int constant;
  
  const Foo(this.eq, this.constant);
  
  // ...
}

class Bar<T> {
  final Equality<T> eq;
  final int constant;
  
  const Bar(this.eq, this.constant);

  // ...
}

Notice how Foo and Bar share T, eq and constant. There's a lot of boilerplate code needed to actually make them share them.

One way to significantly simplify this could be via a declaration that can contain other declarations. Something like the following:

void main() {
  final moduleA = MyFactory<Node>(myNodeEq, 0);
  moduleA.Foo();
  final moduleB = MyFactory<int>(myIntEq, 1);
}

factory MyFactory<T> {
  final Equality<T> eq;
  final int constant;
  
  const MyFactory(this.equality, this.constant);

  class Foo {
    const Foo();
	
    // ...
  }

  class Bar {
    const Bar();

    // ...
  }
}

In this hypothetical "factory" declaration, all members and type parameters of the outer factory would be accessible in Foo and Bar.

I am not aware of any issues that discuss this. This is not meant to be a complete proposal, I just wanted to point out that I think that this is an issue.

It looks like the module system in OCaml has been designed to deal with this issue. I think that something similar would fit Dart very well and could be very useful to people who want to write modular and maintainable code.

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

1 participant