dart-lang / dart-lang/language

User-Defined Constant Functions

Open
#2,222 4 comments 30 reactions 0 assignees View on GitHub
enhanced-const feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Seeing as this idea didn't reveal any glaring flaws when I asked at https://github.com/dart-lang/language/issues/1296#issuecomment-1110098566, I figured I'd make it into a full feature request and see what comes of it.

The suggestion is for users to be able to define their own constant getters, operators, and functions with a restricted set of language features, which can be run at compile time. Naturally, interacting with non-constant/final members of classes wouldn't be possible. Recursion and conditional iteration probably should be prevented too if you don't want to risk the compiler never halting.

It might look something like:
```dart
class Foo
{
final int a;
final int b;

const Foo(this.a, this.b);

const int withC(int c) => (c > 0) ? (a + c) : (b + c);

const int get aPlusB => a + b;

//Operators in particular would be nice, in order to help bridge the gap between system types and others.
const Foo operator+ (Foo other) => Foo(a + other.a, b + other.b);

//Can use string interpolation on primitives if toString is off the table.
static const int _computeB(int x) => "${(x * 180 + 374)}".length;

//Being able to use helper methods in constructors is a good way to split complicated things out from initializer lists, so it'd be cool to be able to do it with constant objects.
const Foo.fromX(int x) : this(x, _computeB(x));
}

class Bar
{
const Foo data = Foo.fromX(23);
const Foo otherData = data + Foo.fromX(3);

static void processData() {
const info = data.aPlusB + otherData.withC(-4); //All computed at compile time.
print(info.toString())
}
}
```

It'd be cool to have full code block bodies with local variables, if and switch statements, maaaaaybe for-each loops, whatever else that can be done without side effects... But I'm not sure how much complexity that'd add, and expressions alone would be make for a helpful addition to the language.

Could make for a partial solution to #2219 - if some of those members could be defined as constant then they wouldn't have to be directly included in the language spec. I imagine some of the more fundamental ones like `List.operator[]` will still need to be, though.

I'm sorta realizing the ramifications and potential complications of a feature like this as I type it up and I'm sure I don't have the complete picture, so I'd be glad to hear others' thoughts on it.

Contributor guide

Open the contributing guide

Research direction

No implementation file or test is named. Start by reviewing the discussion linked from issue #1296 and the related proposal in #2219, then map the language-design implications of user-defined constant getters, operators, and functions. Done would require an agreed, sufficiently specified feature rather than an implementation patch.

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
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.