dart-lang / dart-lang/language

Variable tear off using setters

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

Description

Function and class have tear-off ability why not variable too?

Function
```dart
void function(Function(int value) call) {
call(1);
}

function(function1);
```
And class
```dart
function(Class.new);
```
The suggestion
``` dart
int x = 1;
/// pass as parameter if it have equal callbacks
function(x);
// or
function(x.set);
```

So as mentioned in the dart language tour at [Getters and setters
](https://dart.dev/guides/language/language-tour#methods)
> variable has an implicit getter, plus a setter if appropriate

so let's assume there is var have setter inside of it
I want to tear off the function call back inside var setter

so if I define variable x I think this happens inside
```dart
class ... {
int x = 1;
// inside x
// set x(int value);
// get x => value;
}
```

So my suggestions are

### Suggestion1
```dart
void function(Function(int value) call) {
call(1);
}

void main() {
int x = 1;
function(x);
}
```

### Suggestion2
```dart
void function(Function(int value) call) {
call(1);
}

class TearOff {
int x = 1;
void doThing() {
// set value in x directly using tear off
function(x);
}
}
```

### Suggestion2 old way
```dart
void function(Function(int value) call) {
call(1);
}

class TearOff {
int x = 1;
void doThing() {
function((value) => x = value);
}
}
```

### Suggestion2 another old way
```dart
void function(Function(int value) call) {
call(1);
}

class TearOff {
int x = 1;

void setX(int value) {
x = value;
}

void doThing() {
function(setX);
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the issue examples and the linked Dart language tour section on getters and setters. Compare the proposed variable and setter tear-offs with the existing function and class tear-off behavior. Done would require a resolved language-design decision and corresponding specification work; no implementation files or tests are named.

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
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.