dart-lang / dart-lang/language

Provide a way to gather static initializers

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

Description

Some frameworks need a way to run initialization code for all leaf code using the framework. For example, dependency injection frameworks want to build a map of injectable types, and serialization frameworks want to know about all serializable types.

Currently frameworks either use codegen or manual initialization to do this.

I discussed with @eernstg and @sigurdm; the idea we got to was that it might be possible to build this on top of existing `const` canonicalization support, giving something that's both cheap to implement and sufficient for the use cases we care about.

Leaf code wanting to register itself would declare a `const` in some new special way:

```
@register
const unimportantName = 'value';

@register
const otherUnimportantName = 'otherValue';
```
When the framework is initialized it can ask for all `@register`ed `const` values and iterate over them doing whatever initialization work it wants to.

```
var registrations = Platform.registeredConsts(); // ['value', 'otherValue']
```

This is sufficient for e.g. all injectable types or serializable types to register themselves. Note that the value registered could be any const, e.g. a function, or an instance of a const class. A possible refinement would be to make it possible to ask for all registered consts of a particular type:

```
var registrations = Platform.registeredConsts(); // ['value', 'otherValue']
```

--arguably this removes the need for the `register` annotation since you can request a framework-specific type that is only used for registration.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.