dart-lang / dart-lang/language

Better support for shared modular behaviors.

Open
#3,169 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

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](https://en.wikipedia.org/wiki/Abstract_factory_pattern) where everything is propagated explicitly. Below is a simple example of an abstract factory that consists of just a single implementation:

```dart
void main() {
final moduleA = MyFactory(myNodeEq, 0);
moduleA.fooFactory();
final moduleB = MyFactory(myIntEq, 1);
}

class MyFactory {
final Equality eq;
final int constant;

const MyFactory(this.eq, this.constant);

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

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

class Foo {
final Equality eq;
final int constant;

const Foo(this.eq, this.constant);

// ...
}

class Bar {
final Equality 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:

```dart

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

factory MyFactory {
final Equality 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](https://ocaml.org/docs/modules) 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.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the abstract-factory example and the proposed factory declaration in this issue, then compare the linked OCaml module-system reference. A useful next step would be to define the desired semantics and scope of shared members, type parameters, and nested declarations; done would be a complete, concrete Dart language proposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.