dart-lang / dart-lang/language

A function field should be able to extend a method

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

Description

In TypeScript we can do like this:
```TypeScript
abstract class A {
abstract value(): number;
}

class A1 extends A {
value(): number {
return 0;
}
}

class A2 extends A {
value = (): number => 0;
}

const a1 = new A1();
const a2 = new A2();

console.log(a1.value());
console.log(a2.value());
```

But we can't do like this in Dart, I think it is viable, because when usage, they are the same, there is not much different:
```Dart
abstract class A {
int value();
}

class A1 extends A {
@override
int value() {
return 0;
}
}

//ERROR if extending A
class A2 {
int Function() value = () => 0;
}

void main() {
final a1 = A1();
final a2 = A2();

// using them as fields
log('${a1.value}');
log('${a2.value}');

// using them as functions
log('${a1.value()}');
log('${a2.value()}');
}
```
The output:
```
[log] Closure: () => int from Function 'value':.
[log] Closure: () => int
[log] 0
[log] 0
```

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.